6760.Planets Queries I

Time Limit: 1s Memory Limit: 512MB

You are playing a game consisting of $n$ planets. Each planet has a teleporter to another planet (or the planet itself).

Your task is to process $q$ queries of the form: when you begin on planet $x$ and travel through $k$ teleporters, which planet will you reach?

Input Format(From the terminal/stdin)

The first input line has two integers $n$ and $q$ : the number of planets and queries. The planets are numbered $1,2,\dots,n$ .

The second line has $n$ integers $t_1,t_2,\dots,t_n$ : for each planet, the destination of the teleporter. It is possible that $t_i=i$ .

Finally, there are $q$ lines describing the queries. Each line has two integers $x$ and $k$ : you start on planet $x$ and travel through $k$ teleporters.

  • $1 \le n, q \le 2 \cdot 10^5$
  • $1 \le t_i \le n$
  • $1 \le x \le n$
  • $0 \le k \le 10^9$

Output Format(To the terminal/stdout)

Print the answer to each query.

Sample Input

Copy
4 3
2 1 1 4
1 2
3 4
4 1
 · \n
 · · · \n
 · \n
 · \n
 · \n

Sample Output

Copy
1
2
4
 \n
 \n
 \n
Source: CSES, Graph Algorithms, 1750

Submit

请先 登录

© 2025 FAQs