PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000194. Cộng đỉnh và tính tổng đường đi (Vertex Add Path Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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): 1≤N≤301\le N\le 30; 1≤Q≤1001\le Q\le 100; vertices 0..N−10..N-1 form an undirected tree with N-1 edges; 0≤au,x≤1090\le a_u,x\le10^9; queried vertices have 0≤u,v<N0\le u,v<N; 0 u x adds x, 1 u v queries a path.
  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 1≤Q≤40001\le Q\le 4000; vertices 0..N−10..N-1 form an undirected tree with N-1 edges; 0≤au,x≤1090\le a_u,x\le10^9; queried vertices have 0≤u,v<N0\le u,v<N; 0 u x adds x, 1 u v queries a path.
  • Subtask 3 (50 points): 1≤N≤2000001\le N\le 200000; 1≤Q≤2000001\le Q\le 200000; vertices 0..N−10..N-1 form an undirected tree with N-1 edges; 0≤au,x≤1090\le a_u,x\le10^9; queried vertices have 0≤u,v<N0\le u,v<N; 0 u x adds x, 1 u v queries 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 3+2+4+5=143+2+4+5=14. Adding 5 to vertex 1 changes it to 7, so path 0–1–2 totals 1+7+3=111+7+3=11. Path 2–2 includes one vertex and yields 3. Adding 1 to vertex 4 makes path 4–3–1–2 total 6+4+7+3=206+4+7+3=20.

Operation-by-operation trace

  1. 1 2 4: path contains vertices 2–1–3–4 with values 3+2+4+5; print 14.
  2. 0 1 5: vertex 1 changes from 2 to 7; no output.
  3. 1 0 2: path contains vertices 0–1–2 with values 1+7+3; print 11.
  4. 1 2 2: path contains vertices 2 with values 3; print 3.
  5. 0 4 1: vertex 4 changes from 5 to 6; no output.
  6. 1 4 2: path contains vertices 4–3–1–2 with values 6+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. 1 0 0: path contains vertices 0 with values 100; print 100.
  2. 0 0 7: vertex 0 changes from 100 to 107; no output.
  3. 1 0 0: path contains vertices 0 with values 107; print 107.
  4. 1 0 0: path contains vertices 0 with values 107; print 107.