#SGM0000078. Truy vấn cây con (Subtree Queries)
Truy vấn cây con (Subtree Queries)
Subtree Queries
Source: CSES
Version: Phuoc Hung OJ Extended
Problem
You are given a tree rooted at vertex 1. Each vertex has a value. Process two types of operations: assign a new value to one vertex, and query the sum of values in the entire subtree of a vertex.
Input
The first line contains and . The second line contains the initial vertex values. The next lines contain the tree edges. Each of the following lines is one of:
1 s x: set the value of vertex to .2 s: query the sum of values in the subtree rooted at .
Output
For each operation of type 2, print the requested subtree sum.
Subtasks
Subtask 1 (20%)
- The tree size and number of operations are at most 20.
- All other conditions are the same as Subtask 3.
Subtask 2 (30%)
- The tree size and number of operations are at most 2000.
- All other conditions are the same as Subtask 3.
Subtask 3 (50%)
- .
- .
Example
Input
1 5
840
1 1 671
2 1
2 1
1 1 331
2 1
Output
671
671
331
Explanation
The only vertex is vertex 1, so each type-2 query returns its current value after all preceding assignments.