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

#FT0000192. Cộng đỉnh và tính tổng cây con (Vertex Add Subtree 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 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): 1≤N≤301\le N\le 30; 1≤Q≤1001\le Q\le 100; 0≤au,x≤1090\le a_u,x\le10^9; root 0; 0≤pi<i0\le p_i<i for each i=1..N−1i=1..N-1; commands 0 u x and 1 u have 0≤u<N0\le u<N; type 0 adds x.
  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 1≤Q≤40001\le Q\le 4000; 0≤au,x≤1090\le a_u,x\le10^9; root 0; 0≤pi<i0\le p_i<i for each i=1..N−1i=1..N-1; commands 0 u x and 1 u have 0≤u<N0\le u<N; type 0 adds x.
  • Subtask 3 (50 points): 1≤N≤5000001\le N\le 500000; 1≤Q≤5000001\le Q\le 500000; 0≤au,x≤1090\le a_u,x\le10^9; root 0; 0≤pi<i0\le p_i<i for each i=1..N−1i=1..N-1; commands 0 u x and 1 u have 0≤u<N0\le u<N; 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 10+100+1000+10000=1111010+100+1000+10000=11110; 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 1: subtree contains vertices 1–2–3–4 with values 10+100+1000+10000; print 11110.
  2. 1 2: subtree contains vertices 2–3–4 with values 100+1000+10000; print 11100.
  3. 0 1 100000: vertex 1 changes from 10 to 100010; no output.
  4. 1 0: subtree contains vertices 0–1–2–3–4 with values 1+100010+100+1000+10000; print 111111.
  5. 1 3: subtree contains vertices 3 with values 1000; 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 4+1=54+1=5. Adding 8 to vertex 3 makes the whole-tree total 3+1+4+9=173+1+4+9=17. Subtrees 1 and 3 have sums 1 and 9.

Operation-by-operation trace

  1. 1 2: subtree contains vertices 2–3 with values 4+1; print 5.
  2. 0 3 8: vertex 3 changes from 1 to 9; no output.
  3. 1 0: subtree contains vertices 0–1–2–3 with values 3+1+4+9; print 17.
  4. 1 1: subtree contains vertices 1 with values 1; print 1.
  5. 1 3: subtree contains vertices 3 with values 9; print 9.