Cruel Borders
Time limit1.5sMemory limit1024 MB
Given a tree rooted at state 1 with duties, handle online duty changes and new leaf states, and report what each representative pays while traveling to state 1.
- Level
Hard9 of 10
- Topics
- Tree, Segment tree, Heap, Implementation
- Solved
- No attempts yet
Problem
The states of the European Union can be modeled as a graph in which there is exactly one path between any two states, so the graph is a tree. The states are numbered from 1 to , and Croatia is state 1. This year Mr. Malnar presides over the European Union, so many meetings must be organized. The representatives are odd: they like to travel in groups. On the way to Croatia, everyone passing through a state first gathers in that state. Then they continue to the next state as one group together with that state's representative. There more people join, and this repeats until everyone meets at node 1. (See the explanation of the first sample for details.)
Input
A customs duty on people has been introduced in the European Union. Each state has a duty , and every person entering that state must pay it. The state's own representatives do not pay duty in their own state. The customs officers are cynical about the Union. In each state, the largest group arriving together is charged double the duty. If several groups are equally large, the group coming from the state with the smallest number is charged.
Your program must support three operations:
- : If a meeting were held right now, how much money would the representative of state have to pay?
- : State changes its duty to .
- : A new state appears. Its number is the smallest positive integer that is not yet the number of an existing state. Its duty is , and it is connected to state .
Output
The first line contains and (), the initial number of states and the number of operations. The second line contains integers, where the -th integer is (), the duty of state . Each of the next lines contains and (, ), meaning states and are connected by an edge.
Let be the answer to the most recent operation of type 1, with if there has been no such operation. Let be the largest state number that exists so far. Let denote bitwise xor.
If the -th event is of type 1, the line contains (, ), and .
If the -th event is of type 2 or type 3, the line contains or (, , ), and , .
Hint
For the -th operation of type 1, print the answer on the -th line.
Hint
Explanation of the first input: The fourth operation is the first of type 1, so , and that operation makes no change. The representative of state 2 travels to state 3 and pays the double duty 6, because it is the only group entering that city and therefore the largest. Now the representatives from states 2 and 3 enter state 6 together. That group has two people, while the group from state 7 has only one, so they pay the double duty, meaning the representative of state 2 pays the duty 6. After that, the representatives of states 2, 3, 6, and 7 travel together to state 1 and pay the double duty as the largest group, so the representative of state 2 pays 8. The total is .
In the fifth operation , so . The representative of state 5 travels alone to state 1. It is not the largest group, so it pays the single duty .