#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 NN nodes, numbered from 00 to N1N-1. Node 00 is the root of the tree. Every node except the root has a unique parent. For each ii with 1i<N1 \leq i < N, the parent of node ii is P[i]P[i], where P[i]<iP[i] < i. We define P[0]=1P[0] = -1.

For each node ii (0i<N0 \leq i < N), the subtree of ii is the set of nodes consisting of:

  • ii, and
  • all nodes whose parent is ii, and
  • all nodes whose parent’s parent is ii, and
  • all nodes whose parent’s parent’s parent is ii, and
  • and so on.

The figure below shows an example of a tree with N=6N = 6 nodes. Each arrow goes from a node to its parent (except for the root node, since it has no parent). The subtree of node 22 contains nodes 2,3,42, 3, 4, and 55. The subtree of node 00 contains all 66 nodes of the tree, while the subtree of node 44 contains only node 44 itself.

Each node is assigned a non-negative integer weight. The weight of node ii (0i<N0 \leq i < N) is denoted by W[i]W[i].

Your task is to write a program to answer QQ queries, where each query is represented by a pair of positive integers (L,R)(L, R). 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 C[0],,C[N1]C[0], \ldots, C[N-1], where C[i]C[i] (0i<N0 \leq i < N) is the coefficient assigned to node ii. We call this sequence a coefficient sequence. Note that the elements of a coefficient sequence can be negative, 00, or positive.

For a query (L,R)(L, R), a coefficient sequence is called valid if for every node ii (0i<N0 \leq i < N), the following condition holds: the sum of coefficients in the subtree of node ii is at least LL and at most RR.

Given a coefficient sequence C[0],,C[N1]C[0], \ldots, C[N-1], the cost of node ii is C[i]W[i]|C[i]| \cdot W[i], where C[i]|C[i]| is the absolute value of C[i]C[i]. 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)
  • PP, WW: two integer arrays of length NN, 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)
  • LL, RR: two integers describing one query.
  • For each test case, after init is called, this function will be called QQ 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, L[j]L[j] and R[j]R[j] (0j<Q0 \leq j < Q) are the input parameters for the jj-th call to query. Note that the second line of the input contains only N1N-1 integers, because the sample grader does not read the value of P[0]P[0].

Output Format

The sample grader prints your answers in the following format:

A[0]
A[1]
...
A[Q-1]

Here, A[j]A[j] (0j<Q0 \leq j < Q) is the value returned by the jj-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 33 nodes: the root node and its 22 child nodes. All nodes have weight 11.

query(1, 1)

In this query, L=R=1L = R = 1, which means the sum of coefficients in every subtree must be equal to 11. Consider the coefficient sequence [1,1,1][-1, 1, 1]. The tree and the corresponding coefficients (in the shaded rectangles) are shown below.

For each node ii (0i<30 \leq i < 3), the sum of coefficients over all nodes in the subtree of ii is 11. Therefore, the coefficient sequence is valid. The total cost is computed as follows:

Node Weight Coefficient Cost
00 11 1-1 11=1\mid -1 \mid \cdot 1 = 1
11 11 11=1\mid 1 \mid \cdot 1 = 1
22

Therefore, the total cost is 33. This is the only valid coefficient sequence, so the call should return 33.

query(1, 2)

For this query, the minimum total cost is 22, which can be achieved with the coefficient sequence [0,1,1][0, 1, 1].

Constraints

  • 1N2000001 \leq N \leq 200\,000
  • 1Q1000001 \leq Q \leq 100\,000
  • P[0]=1P[0] = -1
  • For all ii with 1i<N1 \leq i < N, 0P[i]<i0 \leq P[i] < i
  • For all ii with 0i<N0 \leq i < N, 0W[i]10000000 \leq W[i] \leq 1\,000\,000
  • In each query, 1LR10000001 \leq L \leq R \leq 1\,000\,000
Subtask Points Additional Constraints
1 1010 Q10Q \leq 10; for all ii with 1i<N1 \leq i < N, W[P[i]]W[i]W[P[i]] \leq W[i]
2 1313 Q10Q \leq 10; N2000N \leq 2\,000
3 1818 Q10Q \leq 10; N60000N \leq 60\,000
4 77 For all ii with 0i<N0 \leq i < N, W[i]=1W[i] = 1
5 1111 For all ii with 0i<N0 \leq i < N, W[i]1W[i] \leq 1
6 2222 L=1L = 1
7 1919 No additional constraints.

Translated by ChatGPT 5