Home STD Contest Notification Clarification Problems Ranklist Status Print Sign Out

Claris Loves Painting

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 8    Accepted Submission(s): 1


Problem Description
Claris loves painting very much, so he painted a tree with beautiful colors.

The tree is a rooted tree with $n$ nodes which are conveniently labeled by $1,2,...,n$. Its root is the $1$-st node, and the $i$-th node is painted with color $c_i$. If $c_i=c_j$, then we think these two nodes have the same color.

We define $depth_i$ as the distance between the $i$-th node and the root, and simply, the distance between two adjacent nodes is always $1$.

Standing in front of this beautiful tree, Claris comes up with $m$ questions.
In each question, there are two integers $x$ and $d$, which means that Claris wants to know the number of different kinds of colors occur in $S$, where $S=\left\{v|v\ in\ x's\ subtree\ and\ depth_v\leq depth_x+d\right\}$.
 

Input
The first line of the input contains an integer $T(1\leq T\leq 500)$, denoting the number of test cases.

In every test case, there are two integers $n(1\leq n\leq 100000)$ and $m(1\leq m\leq 100000)$ in the first line, denoting the number of nodes and queries.
The second line contains $n$ integers, the $i$-th integer $c_i(1\leq c_i\leq n)$ denotes the color of the $i$-th node.
The third line contains $n-1$ integers, the $i$-th integer $f_{i+1}(1\leq f_i<i)$ denotes the father of the $i+1$-th node.
In the following $m$ lines, each line contains two integers $x(1\leq x\leq n)$ and $d(0\leq d<n)$, denoting each query.

The input has been encoded, if you read $x$ and $d$, the real $x$ and $d$ are $x \oplus last$ and $d \oplus last$, where $last$ is the answer of the previous query and $\oplus$ means bitwise exclusive-or.

Notice :
If it is the first query in this test case, then $last=0$.
It is guaranteed that $\sum n \leq 500000$ and $\sum m \leq 500000$.
 

Output
For each query output a single integer in a line, denoting the answer.
 

Sample Input
1 5 8 1 3 3 2 2 1 1 3 3 1 0 0 0 3 0 1 3 2 1 2 0 6 2 4 1
 

Sample Output
1 2 3 1 1 2 1 1
 

Source
"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场
 

Statistic | Submit | Clarifications | Back