#P8528. [Ynoi2003] 铃原露露

[Ynoi2003] 铃原露露

Background

::::info[题目背景已折叠] ::::

Problem Description

You are given a rooted tree with vertices numbered 1,2,…,n1,2,\dots,n. For 2≤i≤n2\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 l≤L≤R≤rl\le L\le R\le r, and for any L≤ax≤ay≤RL\le a_x\le a_y\le R, if zz is the lowest common ancestor of xx and yy in the tree, then L≤az≤RL\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 n−1n-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, 1≤n,m≤2×1051\le n,m\le 2\times 10^5, 1≤fi≤i−11\le f_i\le i-1, and l≤rl\le r.

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

For another 25%25\% of the testdata, n,m≤3000n,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