#P10574. [JRKSJ R8] 暴风雪
[JRKSJ R8] 暴风雪
Background

Problem Description
The layered city can be abstracted as a tree. You are given a tree with node weights , rooted at node . Initially, all node weights are .
Define as the distance between and on the tree, i.e., the number of edges on the simple path from to .
Let be the subtree rooted at . Define $f(x)=\max_{d\ge 0} \sum_{y\in\text{subtree}(x)} v_y[\text{dis}(x,y)=d]$. In other words, is the maximum, among all depths (levels) in the subtree of , of the sum of node weights on that level.
Now there are operations. In each operation, you are given . First set , and then compute .
Input Format
The first line contains two integers .
The second line contains integers , which give the parent of nodes in order.
The next lines each contain three integers .
Output Format
Output lines in total. Each line contains one integer, the answer for the corresponding operation.
5 7
1 1 1 4
2 1 5
4 2 1
3 4 1
2 5 5
2 4 5
4 4 4
3 2 2
0
6
14
0
0
6
10
6 10
1 1 1 1 2
6 4 1
3 1 1
1 1 1
3 4 1
5 2 1
3 3 1
3 4 1
2 2 1
2 5 1
3 1 1
12
13
13
18
22
28
36
38
46
48
8 10
1 1 2 1 3 3 3
7 3 1
2 4 1
5 2 1
5 2 1
3 1 1
6 2 1
1 4 1
8 4 1
6 4 1
3 2 1
9
14
18
22
23
27
27
35
47
47
Hint
Constraints
This problem uses bundled testdata.
| Special Property | Time Limit | |||
|---|---|---|---|---|
| 1s | ||||
| 4.5s | ||||
For all testdata, , , , .
Translated by ChatGPT 5