This page is still under construction.

Parts of this page are still being built. What you see may change.

Cruel Borders

Time limit1.5sMemory limit1024 MB

Summary
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 nn, 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 ii has a duty cic_i, 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:

  • 11 vv: If a meeting were held right now, how much money would the representative of state vv have to pay?
  • 22 vv cc: State vv changes its duty to cc.
  • 33 vv cc: A new state appears. Its number kk is the smallest positive integer that is not yet the number of an existing state. Its duty is cc, and it is connected to state vv.

Output

The first line contains nn and qq (1≤n,q≤1051 \le n, q \le 10^5), the initial number of states and the number of operations. The second line contains nn integers, where the ii-th integer is cic_i (0≤ci≤1090 \le c_i \le 10^9), the duty of state ii. Each of the next n−1n-1 lines contains uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), meaning states uiu_i and viv_i are connected by an edge.

Let lastanslastans be the answer to the most recent operation of type 1, with lastans=0lastans = 0 if there has been no such operation. Let kk be the largest state number that exists so far. Let ⊕\oplus denote bitwise xor.

If the ii-th event is of type 1, the line contains 11 v′v' (0≤v′≤10180 \le v' \le 10^{18}, 1≤v≤k1 \le v \le k), and v=v′⊕lastansv = v' \oplus lastans.

If the ii-th event is of type 2 or type 3, the line contains 22 v′v' c′c' or 33 v′v' c′c' (0≤v′,c′≤10180 \le v', c' \le 10^{18}, 1≤v≤k1 \le v \le k, 0≤c≤1090 \le c \le 10^9), and v=v′⊕lastansv = v' \oplus lastans, c=c′⊕lastansc = c' \oplus lastans.

Hint

For the ii-th operation of type 1, print the answer on the ii-th line.

Hint

Explanation of the first input: The fourth operation is the first of type 1, so lastans=0lastans = 0, 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 6+6+8=206 + 6 + 8 = 20.

In the fifth operation lastans=20lastans = 20, so v=16⊕20=5v = 16 \oplus 20 = 5. The representative of state 5 travels alone to state 1. It is not the largest group, so it pays the single duty 44.

Examples2

  1. Example 1

    Input
    7 5
    4 6 3 4 0 5 9
    2 3
    3 6
    4 1
    5 1
    1 6
    7 6
    2 5 0
    2 6 3
    3 5 4
    1 2
    1 16
    
    Expected output
    20
    4
    
  2. Example 2

    Input
    5 5
    6 2 2 7 5
    1 3
    2 3
    3 5
    5 4
    3 1 0
    1 6
    1 4
    2 10 11
    1 10
    
    Expected output
    6
    14
    26