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
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.