#FT0000194. Cộng đỉnh và tính tổng đường đi (Vertex Add Path Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cộng đỉnh và tính tổng đường đi (Vertex Add Path Sum)
Vertex Add Path Sum (Cộng đỉnh và tính tổng đường đi)
Source: Library Checker – Vertex Add Path Sum Original statement: https://judge.yosupo.jp/problem/vertex_add_path_sum Version: Phuoc Hung OJ; same problem semantics and maximum source limits, with educational subtasks.
Problem Statement
Given an undirected tree with vertices 0 through N-1 and vertex values a_u, command 0 u x adds x to vertex u; command 1 u v asks for the sum of vertex values on the unique simple path from u to v, including both endpoints. For u=v the result is the current value at that vertex.
Input
First line: N Q; next line: N initial values; next N-1 lines: edges u v; then Q commands 0 u x or 1 u v.
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): ; ; vertices form an undirected tree with N-1 edges; ; queried vertices have ;
0 u xadds x,1 u vqueries a path. - Subtask 2 (30 points): ; ; vertices form an undirected tree with N-1 edges; ; queried vertices have ;
0 u xadds x,1 u vqueries a path. - Subtask 3 (50 points): ; ; vertices form an undirected tree with N-1 edges; ; queried vertices have ;
0 u xadds x,1 u vqueries a path.
Example 1
Input
5 6
1 2 3 4 5
0 1
1 2
1 3
3 4
1 2 4
0 1 5
1 0 2
1 2 2
0 4 1
1 4 2
Output
14
11
3
20
Explanation
Path 2–1–3–4 sums to . Adding 5 to vertex 1 changes it to 7, so path 0–1–2 totals . Path 2–2 includes one vertex and yields 3. Adding 1 to vertex 4 makes path 4–3–1–2 total .
Operation-by-operation trace
1 2 4: path contains vertices2–1–3–4with values3+2+4+5; print 14.0 1 5: vertex 1 changes from 2 to 7; no output.1 0 2: path contains vertices0–1–2with values1+7+3; print 11.1 2 2: path contains vertices2with values3; print 3.0 4 1: vertex 4 changes from 5 to 6; no output.1 4 2: path contains vertices4–3–1–2with values6+4+7+3; print 20.
Example 2
Input
1 4
100
1 0 0
0 0 7
1 0 0
1 0 0
Output
100
107
107
Explanation
The tree consists only of vertex 0. The first query gives 100; after adding 7 its value is 107. The following two queries both return 107.
Operation-by-operation trace
1 0 0: path contains vertices0with values100; print 100.0 0 7: vertex 0 changes from 100 to 107; no output.1 0 0: path contains vertices0with values107; print 107.1 0 0: path contains vertices0with values107; print 107.