#P9336. [Ynoi2001] 梦想歌

[Ynoi2001] 梦想歌

Background

 子供の頃の夢は
 The dreams of childhood.
 色褪せない落書きで
 Are doodles that never fade.
 いつまでも描き続けられた
 Drawn again and again, forever.
 願う未来へとつながる
 Connected tightly to the future we wish for.
 鐘が鳴る音
 The sound of bells ringing.
 遠くから聞こえてくる
 Can be heard even from far away.
 素直な心に
 Reaching an honest heart.
 届いては響いてる
 And then echoing.
 光りは
 Turning into seven-colored.
 七色に変わって
 Light.

Problem Description

You are given a tree with nn nodes, and each node has a weight viv_i.

In this statement, “DSU on tree merging” means: recursively merge sets from bottom to top. Each time, use the sum of node weights in a set as the key, and insert all nodes from the set with the smaller weight sum into the set with the larger weight sum. Initially, each node forms a set containing only itself.

Also, we fix the following enumeration order: assume all subtree merges have already been done recursively. When merging the current node’s level, start from the subtree root, sort its children in increasing order of their indices, and merge sets pairwise each time to obtain the set of the subtree.

Additionally, if two sets have the same weight sum, compare by the minimum node depth inside the set as the second key (merge the one with larger minimum depth into the one with smaller minimum depth).

The root of the tree is fixed to be 11. You need to process the following query and update operations:

1 x means: query the weights of the nodes in the subtree rooted at xx that, after performing DSU on tree merging, have never been involved in an operation of “merging into another set”.

2 x d means: add dd to the weight of node xx.

Input Format

The first line contains two integers n,qn,q, representing the size of the tree and the number of operations.

The second line contains nn integers a1,a2,,ana_1,a_2,\cdots,a_n, where aia_i is the initial weight of node ii.

The third line contains n1n-1 integers p2,p3,,pnp_2,p_3,\cdots,p_n, where pip_i is the parent of node ii when the tree is rooted at node 11.

Then follow qq lines, each in the form 1 x or 2 x d, corresponding to the two operations described above.

Output Format

For each operation of type 11, output one line with one integer, representing the required answer.

5 10
2 10 1 10 3
1 2 3 2
2 1 3
1 3
1 5
2 3 5
2 3 2
1 5
2 5 6
1 3
2 5 -1
2 3 0
10
3
3
10

Hint

Idea: FutaRimeWoawaSete, Solution: zhoukangyang, Code: Rainybunny, Data: FutaRimeWoawaSete/Rainybunny.

Constraints: for 100%100\% of the testdata, 1n,q2×1051\le n,q\le2\times10^5, 1pi<i1\le p_i<i; in operations, x[1,n]x\in[1,n], d[1018,1018]d\in[-10^{18},10^{18}]; at any time, ax1a_x\ge 1 and x=1nax1018\sum_{x=1}^na_x\le10^{18}.

Translated by ChatGPT 5