Where Am I After K Jumps
Each node points to exactly one successor. Answer Q queries: starting at node v and following the pointer K times, which node do you reach?
Input: the first line has N and Q; the second line has the successors; each of the next Q lines has v and K.
Output: one line per query.
Constraints:
- 1 ≤ N, Q ≤ 105
- 0 ≤ K ≤ 1018
Sample Tests
Input 1
4 2 2 3 4 1 1 5 2 2
Output 1
2 4
Input 2
1 1 1 1 1000000000000000000
Output 2
1
Discussion (opens after you solve — no spoilers before)
No comments yet.