#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 1it11\le i\le t-1, dist(vi,vi+1)Cdist(v_i,v_{i+1})\le C.
  4. For any 1it1\le i\le t, lvirl\le v_i\le r.

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

  1. For any aSa\in S and any bb in the complement of SS, aa and bb are not CC-connected.
  2. For any a,bSa,b\in S, aa and bb are CC-connected.
  3. For any aSa\in S, larl\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 n1n-1 numbers 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 numbers l    rl\;\;r, indicating that the interval of this query is [l,r][l,r], and 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. 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:

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 in the ii-th query, l=1,  r=il=1,\;r=i.

Translated by ChatGPT 5