luogu#P5384. [Cnoi2019] 雪松果树

[Cnoi2019] 雪松果树

Background

Gensokyo, winter.

Once a year, the cedar cone trees growing on the high mountains bear fruit again.

Cirno somehow got 1,2,391,2,3\cdots9 cedar cones, then happily ate 66 of them, and in the end only 11 cone was left.

Cirno felt sad because she would not be able to eat cedar cones in the future, so she decided to plant it by the beautiful Misty Lake.

On the first day, it sprouted.

On the second day, the cedar cone tree grew into a towering tree, covered with cedar cones.

Cirno had some questions she wanted to know before the cedar cones ripened, but now she was busy collecting cedar cones, so she threw the questions to you.

Problem Description

The cedar cone tree is a tree with NN nodes rooted at 11.

In addition, Cirno has QQ queries. Each query is an ordered pair (u,k)(u,k), asking how many kk-cousins node uu has.

We define:

The 1-father of node uu is the node on the path (1,u)(1, u) (excluding uu) that is closest to uu.

The kk-father of node uu is the 1-father of the node “the (k1)(k-1)-father of uu”.

The kk-son of node uu is the set of all nodes whose kk-father is uu.

The kk-cousin of node uu is the kk-son of the node “the kk-father of uu” (excluding uu itself).

Input Format

The first line contains two integers NN and QQ.

The second line contains N1N-1 integers. The ii-th integer denotes the 1-father of node i+1i+1.

The following QQ lines each contain an ordered pair (u,k)(u,k).

Output Format

Output one line with QQ numbers, where each number is the answer to a query. If uu does not have a kk-father, output 00.

5 2
1 2 1 4
2 1
3 2
1 1

Hint

Constraints: |Test Point ID|NN\le|QQ\le|Special Property| |----|----|----|-----| |1,2|100100|100100|| |3,4|100100|10610^6|| |5,6|10510^5|100100|| |7|10410^4|50005000|| |8,9,10|10510^5|10510^5|| |11,12,13,14|10610^6|10610^6|The tree is generated randomly| |15,16,17,18,19,20|10610^6|10610^6||

In addition, there is a set of hack testdata worth 2020 points.

Translated by ChatGPT 5