#P9999. [Ynoi2000] tmostnrq2

[Ynoi2000] tmostnrq2

Problem Description

Given a tree with nn vertices, labeled 1,,n1,\dots,n, and a sequence a1,,an0a_1,\dots,a_{n_0} of length n0n_0. There are mm queries. Each query gives l,r,xl,r,x. Starting from vertex xx, move one step toward al,,ara_l,\dots,a_r in order, and output the vertex you finally reach.

If x=yx=y, then moving one step from vertex xx toward yy still stays at xx. Otherwise, you move to the vertex that is adjacent to xx on the tree and is closest to yy.

Input Format

The first line contains three integers n,n0,mn,n_0,m.

The next line contains n1n-1 integers, representing f2,,fnf_2,\dots,f_n in order, where fif_i is the parent of vertex ii, and 11 is the root.

The next line contains n0n_0 integers, representing a1,,an0a_1,\dots,a_{n_0} in order.

The next mm lines each contain three integers l,r,xl,r,x, describing one query.

Output Format

Output mm lines. Each line is the answer to the corresponding query.

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

Hint

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

Constraints: For 100%100\% of the testdata, 1n,n0,m1061\le n,n_0,m\le 10^6.

Translated by ChatGPT 5