XORacci Tree
View as PDFXiao R's favourite operator in coding is the bitwise XOR operator, because his name is very similar to it (XiaO R)! He has also recently learned about Trees and the Fibonacci Sequence. Xiao R has come up with a problem combining all of that, and he is asking for you to help him solve it!
Consider a tree with nodes labelled from
, and an isolated node
, with no edges connected to it. For every
, the value of node
is denoted as
, which is initially set to
. There are two types of operations you can perform on the tree:
Modify operation:
M u x y- Temporarily add an edge between node
and node
.
- Root the entire tree at node
.
- For every node
, calculate a secondary value
such that:
- For all other nodes
:
, where
is the parent of
in the rerooted tree.
- For every
, update
.
- Remove the temporary edge between node
and node
.
- Temporarily add an edge between node
Query operation:
Q v- Output the current value of
.
- Output the current value of
Write a program that given the tree, processes operations.
Note: is the bitwise XOR operator.
Constraints
Subtask 1 [30%]
for all modify operations.
Subtask 2 [70%]
No additional constraints.
Input Specification
The first line will contain two space-separated integers and
.
The next lines will contain two space-separated integers
and
, indicating an edge connecting nodes
and
.
The next lines each contain an operation in the format described above.
Output Specification
For each query operation, output the answer on its own line.
Sample Input
5 4
3 4
2 1
5 3
1 3
M 1 6 5
Q 3
M 3 8 3
Q 0
Sample Output
3
14
Explanation for Sample
For the first modify operation, the rerooted tree is as follows:

The secondary values of nodes ,
, and
are:
,
,
.
The value of is now 6, and the value of
is now
, which is the output for the first query operation.
For the second modify operation, the rerooted tree is as follows:

The secondary value of node 0 is: .
The value of is now 14, which is the output for the second query operation.
Comments