#P7897. [Ynoi2006] spxmcq

[Ynoi2006] spxmcq

Problem Description

Given a rooted tree with nn nodes, the weight of node ii is aia_i.

This tree supports one type of query:

  • Given a node uu and a parameter xx, suppose the weights of all nodes are increased by xx. In this situation, find: among all connected vertex sets that are completely inside the subtree of uu and contain uu, what is the maximum possible sum of weights?

Input Format

The first line contains two positive integers nn and mm.

The second line contains n1n-1 positive integers f2,f3,,fnf_2,f_3,\dots,f_n, which are the parent node indices of nodes 2,3,,n2,3,\dots,n, respectively. It is guaranteed that 1fi<i1\le f_i<i.

The third line contains nn integers a1,a2,,ana_1,a_2,\dots,a_n, which are the weights of nodes 1,2,,n1,2,\dots,n, respectively.

The next mm lines each contain a positive integer uu and an integer xx, representing a query. It is guaranteed that 1un1\le u\le n.

Output Format

Output mm lines, each containing one integer, the answer to the corresponding query.

10 6
1 1 2 2 3 5 5 5 6
5 2 3 1 -5 -7 1 1 1 2
1 0
1 -2
1 3
2 1
5 0
5 -2
11
4
34
7
-2
-7

Hint

Idea: w33z8kqrqk8zzzx33, Solution: w33z8kqrqk8zzzx33&ccz181078, Code: w33z8kqrqk8zzzx33, Data: w33z8kqrqk8zzzx33

For 100%100\% of the testdata, 1n,m1061\le n,m\le 10^6, ai,x108|a_i|,|x|\le 10^8, and it is guaranteed that 1un1\le u\le n.

Translated by ChatGPT 5