QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 2048 MB Total points: 100

#9712. Loli, Yen-Jen, and a cool problem

Statistics

Yen-Jen loves loli very much!!!

Now, he is solving a problem with a cute, cat-eared loli. But he drinks too much yesterday and his mind is not open yet, so he asked you to solve the following problem for him!

Given a rooted tree with $N$ vertices(vertices are numbered from $1$ to $N$) whose root is $1$. In every node, there's an uppercase letter on it.

Define one way to generate a string with length $L$ as follows:

  • select a start node $X$
  • go to the parent node $L - 1$ times
  • whenever goes to a node, append the corresponding English letter to the end of the string

Now there are $Q$ queries, in the $i^{th}$ query, a start node $X_i$ is given. Please count the number of starting node $Y$(include $X_i$), such that $Y$ can generate the same string as $X_i$.

Input

The first line of the input contains two integer $N, Q$ which is mentioned above.

In the next line, a string $S$ is given, the $i^{th}$ character means the English letter on the $i^{th}$ node.

Then, there are $N - 1$ integers in one line, the $i^{th}$ integer is the parent of node $i + 1$.

Then, there are $Q$ lines, the $i^{th}$ line represents the $i^{th}$ query. In each line, $X_i, L_i$ is given, which is mentioned in the description.

It is guaranteed that the testcase is valid!

  • $1 \leq N, Q \leq 3 \times 10 ^ 5$
  • $1 \leq X_i, L_i \leq N$

Output

Output $Q$ lines, the $i^{th}$ represents the ansewr of the $i^{th}$ query.

Sample Input

6 3
ABABBA
1 1 3 3 4
2 2
2 1
6 4

Sample Output

3
3
1

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.