#P10302. [THUWC 2020] 某科学的动态仙人掌

[THUWC 2020] 某科学的动态仙人掌

Problem Description

The title is just to attract you to click in.

You are given a tree whose edge weights are 11, and a constant XX. The nodes are labeled with integers from 11 to nn.

Define dist(a,b)dist(a,b) as the distance between nodes aa and bb on the tree, i.e. the sum of edge weights on the simple path from aa to bb. In particular, dist(a,a)=0dist(a,a)=0.

In each query, an interval [l,r][l,r] is given. You need to ask how many XX-blocks there are, defined as follows.

For any two nodes a,ba,b, define that aa and bb are XX-connected if and only if there exists a node sequence {vi}\{v_i\} of length tt, where tt can be any positive integer, such that:

  1. v1=av_1=a;
  2. vt=bv_t=b;
  3. For any 1it11 \le i \le t-1, dist(vi,vi+1)Xdist(v_i,v_{i+1}) \le X;
  4. For any 1it1 \le i \le t, lvirl \le v_i \le r.

Define an "XX-block" as a set of nodes SS satisfying:

  1. For any aSa \in S and any bb in the complement of SS with lbrl \le b \le r, aa and bb are not XX-connected;
  2. For any a,bSa,b \in S, aa and bb are XX-connected;
  3. For any aSa \in S, larl \le a \le r.

Input Format

The first line contains three integers nn, mm, XX, denoting the number of nodes in the tree, the number of queries, and the constant XX, respectively.

The second line contains n1n-1 integers p2  p3    pnp_2\;p_3\;\dots\;p_n, meaning that for each integer ii with 2in2 \le i \le n, there is an undirected edge between ii and pip_i.

It is guaranteed that the input forms a tree.

Then follow mm lines, each containing two integers l    rl\;\;r, meaning the interval of this query is [l,r][l,r]. It is guaranteed that lrl \le r.

Constraints: 1n31051 \le n \le 3 \cdot 10^5, 1m61051 \le m \le 6 \cdot 10^5.

Output Format

Output mm lines, answering the queries in order. For each query, output one integer per line, which is the answer to this query.

10 9 2
1 1 1 2 3 4 1 1 1
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
5 5

1
1
2
3
3
3
2
1
1

Hint

This problem has multiple subtasks. Each subtask may contain multiple test points, and you can get the score of a subtask only if you pass all test points in that subtask.

The test points of each subtask satisfy some special constraints, as shown in the table below:

Subtask Score nn mm XX Property 1 Property 2
11 44 100100 1010 No No
22 3×1053\times 10 ^ 5 6×1056\times 10 ^ 5 299999299999
33 1616 299900299900
44 11
55 88 22
66 33
77 44
88 1×1051\times 10 ^ 5 3×105\le 3\times 10 ^ 5 Yes
99 44 3×1053\times 10 ^ 5 6×1056\times 10 ^ 5
1010 88 3×1053\times 10 ^ 5 No Yes
1111 44 Yes
1212 88 1×1051\times 10 ^ 5 2×1052\times 10 ^ 5 No
1313 2×1052\times 10 ^ 5 4×1054\times 10 ^ 5
1414 3×1053\times 10 ^ 5 6×1056\times 10 ^ 5

Here, the meanings of Property 1 and Property 2 are as follows.

Property 1: There exists a node ww such that dist(1,w)=n1dist(1,w)=n-1.

Property 2: n=mn=m, and for the ii-th query, l=1,  r=il=1,\;r=i.

Translated by ChatGPT 5