Tree and XOR Queries
Time limit4sMemory limit1024 MB
Maintain a rooted weighted tree under edge-weight updates; for each query, output the sum of path xors d(a,b) over all a in subtree(x) and b in subtree(y).
- Level
Hard8 of 10
- Topics
- Tree, Bit manipulation, DFS, Segment tree
- Solved
- No attempts yet
Problem
A tree is given, rooted at node 1, with weights on its edges. Every node is numbered . For two nodes and , let be the xor of all weights on the simple path from to . If , then .
Write a program that handles the following two queries.
1 : change the weight of the edge from node to the parent of node to .
2 : for any vertex in the subtree of and any vertex in the subtree of , output the sum of all values .
Input
The first line gives the integer , the number of nodes in the tree.
Each of the next lines gives three integers describing an edge. This means the edge connecting node and node in the initial tree has weight . , ,
The next line gives the integer , the number of queries.
The next lines give one query per line in one of the following forms. All inputs are integers.
- 1 ,
- 2
Query type 2 appears at least once, and and may be equal.
Output
Print the result of each type 2 query in order, one per line.
Hint
See the link below for the xor operation.