PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000007. Truy vấn tổng trên cây con (Subtree Queries)
HỌC THEO LỘ TRÌNH
2 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn tổng trên cây con (Subtree Queries)
Subtree Queries
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
There is a tree of vertices rooted at vertex . Vertex stores an integer .
Process operations in order. 1 s x assigns to vertex (it does not add ). 2 s asks for the sum of all values in the subtree rooted at , including vertex .
Each sum query uses all preceding assignments.
Input
- The first line contains .
- The second line contains .
- The next lines contain undirected edges .
- The last lines contain
1 s xor2 s.
Output
For each type-2 query, print the subtree sum on its own line.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; .
Examples
Input
5 3
4 2 5 2 1
1 2
1 3
3 4
3 5
2 3
1 5 3
2 3
Output
8
10
Explanation
Vertex has subtree , initially summing to . Assigning to vertex changes the sum to .