#P9992. [Ynoi Easy Round 2024] TEST_130

[Ynoi Easy Round 2024] TEST_130

Problem Description

Given a rooted tree with nn nodes, define N(w,d)N(w,d) (where ww is a vertex of the tree and dd is a non-negative integer) as the set of nodes in the subtree rooted at ww whose distance to ww is at most dd.

There are mm queries. Each query gives w,dw,d and asks for the size of {N(w,d)N(w,d)N(w,d)}\{N(w',d')\mid N(w',d')\subseteq N(w,d)\}.

Input Format

The first line contains two integers n,mn,m.

The second line contains n1n-1 integers f2,,fnf_2,\dots,f_n, where fif_i denotes the parent of node ii.

The next mm lines each contain two integers w,dw,d, representing one query.

Output Format

Output mm lines. Each line is the answer to the corresponding query in order.

5 4
1 2 2 3
3 1
3 0
5 0
1 2
3
1
1
7

Hint

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

Constraints: For 100%100\% of the data, 1n,m1061\le n,m\le 10^6, 1fi<i1\le f_i<i, 1wn1\le w\le n, 0dn10\le d\le n-1.

For 25%25\% of the data, n,m100n,m\le 100.

For another 25%25\% of the data, n,m5000n,m\le 5000.

For another 25%25\% of the data, n,m2×105n,m\le 2\times 10^5.

For the remaining 25%25\% of the data, there are no special restrictions.

Translated by ChatGPT 5