#FT0000192. Cộng đỉnh và tính tổng cây con (Vertex Add Subtree Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cộng đỉnh và tính tổng cây con (Vertex Add Subtree Sum)
Vertex Add Subtree Sum (Cộng đỉnh và tính tổng cây con)
Source: Library Checker – Vertex Add Subtree Sum Original statement: https://judge.yosupo.jp/problem/vertex_add_subtree_sum Version: Phuoc Hung OJ; same problem semantics and maximum source limits, with educational subtasks.
Problem Statement
A rooted tree has vertices 0 through N-1 and root 0. Vertex i initially has value a_i; every parent p_i (i≥1) satisfies 0≤p_i<i. Command 0 u x adds x to a_u; command 1 u asks for the sum of values in the subtree rooted at u, including u. The tree structure is fixed.
Input
Line 1: N Q; line 2: N initial values a_0...a_{N-1}; line 3: parents p_1...p_{N-1} (empty if N=1); followed by Q operations 0 u x or 1 u.
Output
For each query operation, print exactly one integer on its own line in query order. Updates produce no output. Some sums exceed 32-bit integers; use signed 64-bit integers.
Subtasks
- Subtask 1 (20 points): ; ; ; root 0; for each ; commands
0 u xand1 uhave ; type 0 adds x. - Subtask 2 (30 points): ; ; ; root 0; for each ; commands
0 u xand1 uhave ; type 0 adds x. - Subtask 3 (50 points): ; ; ; root 0; for each ; commands
0 u xand1 uhave ; type 0 adds x.
Example 1
Input
5 5
1 10 100 1000 10000
0 1 2 2
1 1
1 2
0 1 100000
1 0
1 3
Output
11110
11100
111111
1000
Explanation
The subtree at vertex 1 sums to ; vertex 2 sums to 11100. Adding 100000 to vertex 1 makes the whole-tree sum 111111. Vertex 3 remains 1000.
Operation-by-operation trace
1 1: subtree contains vertices1–2–3–4with values10+100+1000+10000; print 11110.1 2: subtree contains vertices2–3–4with values100+1000+10000; print 11100.0 1 100000: vertex 1 changes from 10 to 100010; no output.1 0: subtree contains vertices0–1–2–3–4with values1+100010+100+1000+10000; print 111111.1 3: subtree contains vertices3with values1000; print 1000.
Example 2
Input
4 5
3 1 4 1
0 0 2
1 2
0 3 8
1 0
1 1
1 3
Output
5
17
1
9
Explanation
The root 0 has children 1 and 2, and vertex 2 has child 3. Subtree 2 initially sums to . Adding 8 to vertex 3 makes the whole-tree total . Subtrees 1 and 3 have sums 1 and 9.
Operation-by-operation trace
1 2: subtree contains vertices2–3with values4+1; print 5.0 3 8: vertex 3 changes from 1 to 9; no output.1 0: subtree contains vertices0–1–2–3with values3+1+4+9; print 17.1 1: subtree contains vertices1with values1; print 1.1 3: subtree contains vertices3with values9; print 9.