P9992 [Ynoi Easy Round 2024] TEST_130

Description

Given a rooted tree with $n$ nodes, define $N(w,d)$ (where $w$ is a vertex of the tree and $d$ is a non-negative integer) as the set of nodes in the subtree rooted at $w$ whose distance to $w$ is at most $d$. There are $m$ queries. Each query gives $w,d$ and asks for the size of $\{N(w',d')\mid N(w',d')\subseteq N(w,d)\}$.

Input Format

The first line contains two integers $n,m$. The second line contains $n-1$ integers $f_2,\dots,f_n$, where $f_i$ denotes the parent of node $i$. The next $m$ lines each contain two integers $w,d$, representing one query.

Output Format

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

Explanation/Hint

Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078. Constraints: For $100\%$ of the data, $1\le n,m\le 10^6$, $1\le f_i