#P8528. [Ynoi2003] 铃原露露
[Ynoi2003] 铃原露露
Background

Problem Description
You are given a rooted tree with vertices numbered . For , is the parent of . is a permutation of .
There are queries. Each query gives and asks how many pairs satisfy , and for any , if is the lowest common ancestor of and in the tree, then .
All values above are integers.
Input Format
The first line contains two integers .
The next line contains integers .
The next lines, in order, give .
The next lines each contain , describing one query.
Output Format
For each query, output one line containing the answer.
5 5
2 5 1 3 4
1
2
3
4
1 1
1 4
3 3
2 2
1 1
1
10
1
1
1
Hint
Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078.
For of the testdata, , , and .
For of the testdata, .
For another of the testdata, .
For another of the testdata, .
For another of the testdata, there are no special constraints.
Translated by ChatGPT 5