#P8528. [Ynoi2003] 铃原露露

[Ynoi2003] 铃原露露

Background

Problem Description

You are given a rooted tree with vertices numbered 1,2,,n1,2,\dots,n. For 2in2\le i\le n, fif_{i} is the parent of ii. a1,,ana_1,\dots,a_n is a permutation of 1,,n1,\dots,n.

There are mm queries. Each query gives l,rl,r and asks how many pairs (L,R)(L,R) satisfy lLRrl\le L\le R\le r, and for any LaxayRL\le a_x\le a_y\le R, if zz is the lowest common ancestor of xx and yy in the tree, then LazRL\le a_z\le R.

All values above are integers.

Input Format

The first line contains two integers n mn\ m.

The next line contains nn integers a1,,ana_1,\dots,a_n.

The next n1n-1 lines, in order, give f2,,fnf_2,\dots,f_n.

The next mm lines each contain l rl\ r, 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 100%100\% of the testdata, 1n,m2×1051\le n,m\le 2\times 10^5, 1fii11\le f_i\le i-1, and lrl\le r.

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

For another 25%25\% of the testdata, n,m3000n,m\le 3000.

For another 25%25\% of the testdata, l=1,  r=n,  m=1l=1,\;r=n,\;m=1.

For another 25%25\% of the testdata, there are no special constraints.

Translated by ChatGPT 5