#P7126. [Ynoi2008] rdCcot

[Ynoi2008] rdCcot

Problem Description

Given a tree whose edge weights are all 11 and a constant CC. The nodes are labeled by 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 CC-blocks there are, defined as follows:

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

  1. v1=av_1=a.
  2. vt=bv_t=b.
  3. For any 1≤i≤t−11\le i\le t-1, dist(vi,vi+1)≤Cdist(v_i,v_{i+1})\le C.
  4. For any 1≤i≤t1\le i\le t, l≤vi≤rl\le v_i\le r.

Define a "CC-block" as a set of nodes SS such that:

  1. For any a∈Sa\in S and any bb in the complement of SS, aa and bb are not CC-connected.
  2. For any a,b∈Sa,b\in S, aa and bb are CC-connected.
  3. For any a∈Sa\in S, l≤a≤rl\le a\le r.

Input Format

The first line contains three numbers nn, mm, CC, representing the number of nodes in the tree, the number of queries, and the constant CC, respectively.

The second line contains n−1n-1 numbers p2  p3  …  pnp_2\;p_3\;\dots\;p_n, meaning that for each integer ii with 2≤i≤n2 \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 numbers l    rl\;\;r, indicating that the interval of this query is [l,r][l,r], and it is guaranteed that l≤rl \le r.

Constraints: 1≤n≤3⋅1051 \le n\le 3\cdot 10^5, 1≤m≤6⋅1051 \le m\le 6\cdot 10^5.

Output Format

Output mm lines, answering the queries in order. Each line contains one integer, representing the answer to that 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

Idea: nzhtl1477, Solution: ccz181078, Code: nzhtl1477&ccz181078, Data: ccz181078.

This problem has multiple subtasks. Each subtask may contain multiple test points. 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 restrictions, as shown in the table below:

Subtask Score nn mm CC Additional Constraints 1 Additional Constraints 2
1 1 4 4 100 100 10 10 No
2 2 300000 300000 600000 600000 299999 299999 ^ ^
3 3 16 16 ^ 299900 299900
4 4 1 1
5 5 8 8 2 2
6 6 3 3
7 7 4 4
8 8 100000 100000 300000 300000 Yes
9 9 4 4 300000 300000 600000 600000 ^
10 10 8 8 300000 300000 ≤300000 \leq 300000 No Yes
11 11 4 4 ^ ^ Yes ^
12 12 8 8 100000 100000 200000 200000 No
13 13 200000 200000 400000 400000 ^
14 14 300000 300000 600000 600000

The meanings of Additional Constraints 1 and Additional Constraints 2 are as follows:

Additional Constraints 1: There exists a node ww such that dist(1,w)=n−1dist(1,w)=n-1.

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

Translated by ChatGPT 5