In a tree \(T\) with \(N\) nodes, each node has a value \(A[i]\). You are given \(Q\) queries of the form \(u \ \ v\). For each query, we need to find the value of function \(F\).
Function \(F\) is defined as the sum of the Bitwise OR and XOR operations on the values of nodes in a simple path between nodes \(u\) and \(v\).
To simplify, you need to calculate the sum of OR and XOR of the values of nodes on the path between \(u\) and \(v\) for each query.
Input Format:
- The first line contains two space-separated integers \(N\) and \(Q\).
- The next \(N-1\) lines contain two space-separated integers \(u \ \ v\) denoting the edge between node \(u\) and node \(v\).
- The next line contains \(N\) space-separated integers denoting the value of nodes.
- The next \(Q\) lines contain two space-separated integers, \(u \ \ v\).
Output Format:
For every query output the value of function \(F\)
Constraints:
- OR(1,6) = 1 | 2 | 4 | 8 | 16 | 32 = 63
- XOR(1,6) = 63
So, for Query 1 , F = 63 + 63 = 126
Similarly for Query 2 , F = 3 + 3 = 6
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor