#P7897. [Ynoi2006] spxmcq
[Ynoi2006] spxmcq
Problem Description
Given a rooted tree with nodes, the weight of node is .
This tree supports one type of query:
- Given a node and a parameter , suppose the weights of all nodes are increased by . In this situation, find: among all connected vertex sets that are completely inside the subtree of and contain , what is the maximum possible sum of weights?
Input Format
The first line contains two positive integers and .
The second line contains positive integers , which are the parent node indices of nodes , respectively. It is guaranteed that .
The third line contains integers , which are the weights of nodes , respectively.
The next lines each contain a positive integer and an integer , representing a query. It is guaranteed that .
Output Format
Output 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 of the testdata, , , and it is guaranteed that .
Translated by ChatGPT 5