QOJ.ac

QOJ

Time Limit: 12 s Memory Limit: 512 MB Total points: 100

#11275. tmostnrq2

Statistics

Given a tree with $n$ vertices labeled $1, \dots, n$, and a sequence $a_1, \dots, a_{n_0}$ of length $n_0$, there are $m$ queries. Each query provides $l, r, x$, and asks for the vertex reached starting from vertex $x$ by moving one step towards each of $a_l, \dots, a_r$ in sequence.

If $x = y$, moving one step from $x$ towards $y$ results in $x$. Otherwise, it results in the vertex adjacent to $x$ that is closest to $y$ in the tree.

Input

The first line contains three integers $n, n_0, m$.

The next line contains $n-1$ integers representing $f_2, \dots, f_n$, where $f_i$ is the parent of vertex $i$, and $1$ is the root.

The next line contains $n_0$ integers representing $a_1, \dots, a_{n_0}$.

The next $m$ lines each contain three integers $l, r, x$ representing a query.

Output

Output $m$ lines, each containing the answer for the corresponding query.

Examples

Input 1

5 4 3
1 1 3 3
5 2 2 3
3 4 5
1 3 4
1 2 1

Output 1

3
2
1

Subtasks

Idea: Ynoi, Solution: zhoukangyang & ccz181078, Code: zhoukangyang, Data: ccz181078

For $100\%$ of the data, $1 \le n, n_0, m \le 10^6$.

Discussions

About Discussions

The discussion section is only for posting: 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.
  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.