#PS0000316. Cây thay đổi giá trị (Dynamic Subtree Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cây thay đổi giá trị (Dynamic Subtree Sum)
Dynamic Subtree Sum
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices numbered from to and is rooted at vertex . Initially, vertex has integer value .
The subtree rooted at consists of and every vertex whose path from root passes through . The edges never change.
Process the following operations in input order:
1 v x: assign to vertex (do not add ).2 v: report the sum of current values of all vertices in the subtree rooted at .
Input
- The first line contains integers , the number of vertices and operations, respectively.
- The second line contains exactly integers ; the -th integer is the initial value of vertex .
- Each of the next lines contains integers , the endpoints of an undirected edge.
- The last lines describe operations in execution order. If the first integer is , the line also contains vertex and its new value . If it is , the line contains only one more integer, the queried vertex .
Output
Print one integer per type-2 operation, in query order. If there are no type-2 operations, print nothing.
Subtasks
- Subtask 1 (20 points): ; ; ; The edges form an undirected tree without duplicate edges or loops. All operations are type 1 or 2. .
- Subtask 2 (30 points): ; ; ; The edges form an undirected tree without duplicate edges or loops. All operations are type 2.
- Subtask 3 (50 points): ; ; ; The edges form an undirected tree without duplicate edges or loops. All operations are type 1 or 2.
Example 1
Input
5 6
3 -2 4 7 -1
1 2
1 3
2 4
2 5
2 2
1 4 -5
2 2
2 1
1 2 10
2 2
Output
4
-8
-1
4
Explanation
Subtree 2 contains vertices 2, 4, 5, initially totaling . Assigning to vertex 4 changes that sum to . The whole tree sums to . Assigning 10 to vertex 2 gives .
Example 2
Input
1 4
-8
2 1
1 1 0
1 1 0
2 1
Output
-8
0
Explanation
The only vertex is vertex , initially valued at . The first query returns . The next two operations both assign to this vertex, so the second assignment changes nothing. The final query returns .