#P10574. [JRKSJ R8] 暴风雪

    ID: 10230 远端评测题 1000~4500ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2024洛谷原创O2优化洛谷月赛

[JRKSJ R8] 暴风雪

Background

Problem Description

The layered city can be abstracted as a tree. You are given a tree with node weights viv_i, rooted at node 11. Initially, all node weights viv_i are 00.

Define dis(x,y)\text{dis}(x,y) as the distance between xx and yy on the tree, i.e., the number of edges on the simple path from xx to yy.

Let subtree(x)\text{subtree}(x) be the subtree rooted at xx. Define $f(x)=\max_{d\ge 0} \sum_{y\in\text{subtree}(x)} v_y[\text{dis}(x,y)=d]$. In other words, f(x)f(x) is the maximum, among all depths (levels) in the subtree of xx, of the sum of node weights on that level.

Now there are mm operations. In each operation, you are given x,w,yx,w,y. First set vxvx+wv_x\gets v_x+w, and then compute isubtree(y)f(i)\sum_{i\in \text{subtree}(y)} f(i).

Input Format

The first line contains two integers n,mn,m.

The second line contains n1n-1 integers f2nf_{2\dots n}, which give the parent of nodes 2,3,,n2,3,\dots,n in order.

The next mm lines each contain three integers x,w,yx,w,y.

Output Format

Output mm 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.

Subtask\text{Subtask} n,mn,m\le Special Property Score\text{Score} Time Limit
11 100100 55 1s
22 50005000 1515
33 3×1053\times10^5 fi=i1f_i=i-1 1010 4.5s
44 7×1047\times 10^4 2020
55 3×1053\times10^5 5050

For all testdata, 1n,m3×1051\le n,m\le3\times 10^5, 1x,yn1\le x,y\le n, 1w1081\le w \le 10^8, 1fin1\le f_i\le n.

Translated by ChatGPT 5