Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Touching Grass

Input file: standard input


Output file: standard output
Time limit: 2 seconds
Memory limit: 1024 megabytes

Chicken managed to crack the binary lock just in the nick of time, and he is now free!
After escaping, he decided to move to a small town known for its lush green grass. This town consists of
n districts, numbered from 1 to n, connected by n − 1 roads such that it is possible to travel between any
two districts using only these roads.
Equipped with his newfound knowledge of finding grass, chicken is thrilled to finally experience the
sensation of touching grass! He learns that each district i currently has ai grass, but this may change as
new grass grows and old grass wilts. He wants to touch as much grass as possible, but does not want to
venture too far from his home.
To determine which district he should live in to best satisfy his grass touching needs, chicken asks you to
help him process the following operations:

• 0 x k: Query the total amount of grass in districts within a distance of k from chicken’s home,
assuming he lives in district x.

• 1 x y: Update the amount of grass in district x to y.

Furthermore, as chicken is getting restless, he wants you to answer his queries immediately. Note that the
distance between two districts is the number of roads on the shortest path connecting them.

Input
The first line contains two integers n and m — the number of districts and the number of operations.
The second line contains n integers a1 , a2 , . . . , an — the initial amount of grass in each district.
Each of the next n − 1 lines contain two integers, the i-th line containing ui and vi , denoting a road
between districts ui and vi .
Each of the next m lines contain either 0 x’ k’ or 1 x’ y’, representing an operation described above. To
obtain the actual values of x, y and k, take x = x0 ⊕ lastans, y = y 0 ⊕ lastans and k = k 0 ⊕ lastans,
where ⊕ is the bitwise XOR operation and lastans is the answer to the previous query operation.
Initially, lastans = 0.

Output
For each query operation, output the corresponding answer in a separate line.

Scoring
For all subtasks, it is guaranteed that:

• 1 ≤ n, m ≤ 105

• 1 ≤ ui , vi , x ≤ n

• 1 ≤ ai , y ≤ 104

• 0≤k ≤n−1

Page 1 of 2
Subtask Score Additional Constraints
1 12 n, m ≤ 2000
2 17 For all query operations, x = 1
3 40 The town forms a complete binary tree, ui = b i+1
2 c, vi = i + 1
4 31 —
5 0 Sample Testcases

Example
standard input standard output
5 4 8
4 1 3 7 2 19
1 2 2
1 3
2 4
2 5
0 1 1
1 11 13
0 9 11
0 22 19

Note
In the first operation, x = 1 and k = 1. The districts within distance 1 from district 1 are districts 1, 2,
3. In total, there is 4 + 1 + 3 = 8 grass.
In the second operation, x = 3 and y = 5. The amount of grass in district 3 is updated to 5.
In the third operation, x = 1 and k = 3. The districts within distance 3 from district 1 are districts 1, 2,
3, 4, 5. In total, there is 4 + 1 + 5 + 7 + 2 = 19 grass.
In the fourth operation, x = 5 and k = 0. The only district within distance 0 from district 5 is district 5.
In total, there is 2 grass.

Page 2 of 2

You might also like