#P11051. [IOI 2024] 树上代价
[IOI 2024] 树上代价
Background
When submitting, please do not include tree.h.
Please do not submit using C++14 (GCC 9).
Problem Description
There is a tree with nodes, numbered from to . Node is the root of the tree. Every node except the root has a unique parent. For each with , the parent of node is , where . We define .
For each node (), the subtree of is the set of nodes consisting of:
- , and
- all nodes whose parent is , and
- all nodes whose parent’s parent is , and
- all nodes whose parent’s parent’s parent is , and
- and so on.
The figure below shows an example of a tree with nodes. Each arrow goes from a node to its parent (except for the root node, since it has no parent). The subtree of node contains nodes , and . The subtree of node contains all nodes of the tree, while the subtree of node contains only node itself.

Each node is assigned a non-negative integer weight. The weight of node () is denoted by .
Your task is to write a program to answer queries, where each query is represented by a pair of positive integers . The answer to each query should be computed as follows.
For every node in the tree, assign an integer called a coefficient. Such an assignment is described by a sequence , where () is the coefficient assigned to node . We call this sequence a coefficient sequence. Note that the elements of a coefficient sequence can be negative, , or positive.
For a query , a coefficient sequence is called valid if for every node (), the following condition holds: the sum of coefficients in the subtree of node is at least and at most .
Given a coefficient sequence , the cost of node is , where is the absolute value of . The total cost is the sum of the costs of all nodes. For each query, your task is to compute the minimum total cost that can be achieved by some valid coefficient sequence.
It can be proven that for every query, there exists at least one valid coefficient sequence.
Implementation Details
You need to implement the following two functions:
void init(std::vector<int> P, std::vector<int> W)
- , : two integer arrays of length , recording the parent and weight of each node.
- For each test case, this function will be called exactly once when the grader starts interacting with your program.
long long query(int L, int R)
- , : two integers describing one query.
- For each test case, after
initis called, this function will be called times. - This function should return the answer for the given query.
Input Format
The sample grader reads input in the following format:
N
P[1] P[2] ... P[N-1]
W[0] W[1] ... W[N-2] W[N-1]
Q
L[0] R[0]
L[1] R[1]
...
L[Q-1] R[Q-1]
Here, and () are the input parameters for the -th call to query. Note that the second line of the input contains only integers, because the sample grader does not read the value of .
Output Format
The sample grader prints your answers in the following format:
A[0]
A[1]
...
A[Q-1]
Here, () is the value returned by the -th call to query.
3
0 0
1 1 1
2
1 1
1 2
3
2
Hint
Consider the following call:
init([-1, 0, 0], [1, 1, 1])
This tree has nodes: the root node and its child nodes. All nodes have weight .
query(1, 1)
In this query, , which means the sum of coefficients in every subtree must be equal to . Consider the coefficient sequence . The tree and the corresponding coefficients (in the shaded rectangles) are shown below.

For each node (), the sum of coefficients over all nodes in the subtree of is . Therefore, the coefficient sequence is valid. The total cost is computed as follows:
| Node | Weight | Coefficient | Cost |
|---|---|---|---|
Therefore, the total cost is . This is the only valid coefficient sequence, so the call should return .
query(1, 2)
For this query, the minimum total cost is , which can be achieved with the coefficient sequence .
Constraints
- For all with ,
- For all with ,
- In each query,
| Subtask | Points | Additional Constraints |
|---|---|---|
| 1 | ; for all with , | |
| 2 | ; | |
| 3 | ; | |
| 4 | For all with , | |
| 5 | For all with , | |
| 6 | ||
| 7 | No additional constraints. |
Translated by ChatGPT 5