luogu#P7889. 「MCOI-06」Eert Tuc Knil
「MCOI-06」Eert Tuc Knil
Problem Description
Given a rooted tree with nodes, the weight of node is .
This tree supports the following type of query:
- Given a node and a parameter , suppose the weights of all nodes are increased by . Under this assumption, find: among all connected sets of nodes that are completely inside the subtree of and that 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, and it is guaranteed that .
The third line contains integers , which are the node weights of nodes respectively.
The next lines each contain a positive integer and an integer , representing one query. It is guaranteed that .
Output Format
Output lines, each containing one integer, which is 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
Constraints
This problem uses bundled testdata.
- Subtask 1 (5 pts): .
- Subtask 2 (10 pts): and .
- Subtask 3 (15 pts): .
- Subtask 4 (47 pts): .
- Subtask 5 (23 pts): no special restrictions.
For all testdata, , , and it is guaranteed that .
Translated by ChatGPT 5