PivotOJ

Tree

시간 제한: 2000ms메모리 제한: 1024MB출처: IOI 2024BOJ 32264

문제

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 1 &le; 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 (0 &le; 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 22, 33, 44 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 (0 &le; 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],\dots ,C[N - 1], where C[i]C[i] (0 &le; 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 (0 &le; 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],\dots ,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.

코드를 제출하려면 로그인하세요.