View problem - Tree (IOI24_tree)

Time limitMemory limit# of submissions# of submitted usersSolved #Accepted user ratio
2000 ms2048 MiB129291034.48%

Consider a tree consisting of NN vertices, numbered from 00 to N1N-1. Vertex 00 is called the root. Every vertex, except for the root, has a single parent. For every ii, such that 1i<N1 \leq i < N, the parent of vertex ii is vertex P[i]P[i], where P[i]<iP[i] < i. We also assume P[0]=1P[0] = -1.

For any vertex ii (0i<N0 \leq i < N), the subtree of ii is the set of the following vertices:

  • ii, and
  • any vertex whose parent is ii, and
  • any vertex whose parent's parent is ii, and
  • any vertex whose parent's parent's parent is ii, and
  • etc.

The picture below shows an example tree consisting of N=6N = 6 vertices. Each arrow connects a vertex to its parent, except for the root, which has no parent. The subtree of vertex 22 contains vertices 2,3,42, 3, 4 and 55. The subtree of vertex 00 contains all 66 vertices of the tree and the subtree of vertex 44 contains only vertex 44.

Each vertex is assigned a nonnegative integer weight. We denote the weight of vertex ii (0i<N0 \leq i < N) by W[i]W[i].

Your task is to write a program that will answer QQ queries, each specified by a pair of positive integers (L,R)(L, R). The answer to the query should be computed as follows.

Consider assigning an integer, called a coefficient, to each vertex of the tree. 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 vertex ii. Let us call this sequence a coefficient sequence. Note that the elements of the coefficient sequence can be negative, 00, or positive.

For a query (L,R)(L, R), a coefficient sequence is called valid if, for every vertex ii (0i<N0 \leq i < N), the following condition holds: the sum of the coefficients of the vertices in the subtree of vertex ii is not less than LL and not greater than RR.

For a given coefficient sequence C[0],,C[N1]C[0], \ldots, C[N-1], the cost of a vertex ii is C[i]W[i]|C[i]| \cdot W[i], where C[i]|C[i]| denotes the absolute value of C[i]C[i]. Finally, the total cost is the sum of the costs of all vertices. Your task is to compute, for each query, the minimum total cost that can be attained by some valid coefficient sequence.

It can be shown that for any query, at least one valid coefficient sequence exists.

Implementation Details

You should implement the following two procedures:

void init(std::vector<int> P, std::vector<int> W)
  • PP, WW: arrays of integers of length NN specifying the parents and the weights.
  • This procedure is called exactly once in the beginning of the interaction between the grader and your program in each test case.
long long query(int L, int R)
  • LL, RR: integers describing a query.
  • This procedure is called QQ times after the invocation of init in each test case.
  • This procedure should return the answer to the given query.

Constraints

  • 1N200,0001 \leq N \leq 200,000
  • 1Q100,0001 \leq Q \leq 100,000
  • P[0]=1P[0] = -1
  • 0P[i]<i0 \leq P[i] < i for each ii such that 1i<N1 \leq i < N
  • 0W[i]1,000,0000 \leq W[i] \leq 1,000,000 for each ii such that 0i<N0 \leq i < N
  • 1LR1,000,0001 \leq L \leq R \leq 1,000,000 in each query

Subtasks

Subtask Score Additional Constraints
1 1010 Q10Q \leq 10; W[P[i]]W[i]W[P[i]] \leq W[i] for each ii such that 1i<N1 \leq i < N
2 1313 Q10Q \leq 10; N2,000N \leq 2,000
3 1818 Q10Q \leq 10; N60,000N \leq 60,000
4 77 W[i]=1W[i] = 1 for each ii such that 0i<N0 \leq i < N
5 1111 W[i]1W[i] \leq 1 for each ii such that 0i<N0 \leq i < N
6 2222 L=1L = 1
7 1919 No additional constraints.

Examples

Consider the following calls:

init([-1, 0, 0], [1, 1, 1])

The tree consists of 33 vertices, the root and its 22 children. All vertices 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 shaded rectangles) are illustrated below.

For every vertex ii (0i<30 \leq i < 3), the sum of the coefficients of all vertices in the subtree of ii is equal to 11. Hence, this coefficient sequence is valid. The total cost is computed as follows:

Vertex Weight Coefficient Cost
0 1 -1 11=1\mid -1 \mid \cdot 1 = 1
1 1 1 11=1\mid 1 \mid \cdot 1 = 1
2 1 1 11=1\mid 1 \mid \cdot 1 = 1

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

query(1, 2)

The minimum total cost for this query is 22, and is attained when the coefficient sequence is [0,1,1][0, 1, 1].

Sample Grader

Input 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]

where L[j]L[j] and R[j]R[j] (for 0j<Q0 \leq j < Q) are the input arguments in the jj-th call to query. Note that the second line of the input contains only N1N-1 integers, as the sample grader does not read the value of P[0]P[0].

Output format:

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

where A[j]A[j] (for 0j<Q0 \leq j < Q) is the value returned by the jj-th call to query.

Attachments
File nameSize
tree.zip2.39 KiB