#P9999. Stepwise Tree Navigation
Stepwise Tree Navigation
Stepwise Tree Navigation
Given a tree with (n) vertices labeled from (1) to (n) and a sequence (a_1, \dots, a_{n_0}), process (m) queries. For each query, given integers (l, r, x), start at vertex (x) and, for each index from (l) to (r) in the sequence, move one step toward vertex (a_i) along the unique path in the tree. The movement rule is as follows: if the current vertex (x = a_i), remain at (x); otherwise, move to the adjacent vertex on the path from (x) to (a_i) that is closest to (a_i). Output the final vertex reached after executing the moves.
inputFormat
The first line contains three integers (n), (n_0), and (m). The next (n-1) lines each contain two integers (u) and (v), which represent an undirected edge in the tree. The following line contains (n_0) integers denoting the sequence (a_1, \dots, a_{n_0}). Each of the next (m) lines contains three integers (l, r, x), representing a query.
outputFormat
For each query, output the final vertex after performing the series of moves. Each answer should be printed on a new line.
sample
5 5 2
1 2
2 3
2 4
4 5
2 3 4 5 1
1 3 1
2 5 4
2
4
</p>