#P1619H. Permutation and Queries

    ID: 147 Type: RemoteJudge 4000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>brute forcedata structuresdivide and conquertwo pointers*2400

Permutation and Queries

No submission language available for this problem.

Description

You are given a permutation pp of nn elements. A permutation of nn elements is an array of length nn containing each integer from 11 to nn exactly once. For example, [1,2,3][1, 2, 3] and [4,3,5,1,2][4, 3, 5, 1, 2] are permutations, but [1,2,4][1, 2, 4] and [4,3,2,1,2][4, 3, 2, 1, 2] are not permutations. You should perform qq queries.

There are two types of queries:

  • 11 xx yy — swap pxp_x and pyp_y.
  • 22 ii kk — print the number that ii will become if we assign i=pii = p_i kk times.

The first line contains two integers nn and qq (1n,q1051 \le n, q \le 10^5).

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n.

Each of the next qq lines contains three integers. The first integer is tt (1t21 \le t \le 2) — type of query. If t=1t = 1, then the next two integers are xx and yy (1x,yn1 \le x, y \le n; xyx \ne y) — first-type query. If t=2t = 2, then the next two integers are ii and kk (1i,kn1 \le i, k \le n) — second-type query.

It is guaranteed that there is at least one second-type query.

For every second-type query, print one integer in a new line — answer to this query.

Input

The first line contains two integers nn and qq (1n,q1051 \le n, q \le 10^5).

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n.

Each of the next qq lines contains three integers. The first integer is tt (1t21 \le t \le 2) — type of query. If t=1t = 1, then the next two integers are xx and yy (1x,yn1 \le x, y \le n; xyx \ne y) — first-type query. If t=2t = 2, then the next two integers are ii and kk (1i,kn1 \le i, k \le n) — second-type query.

It is guaranteed that there is at least one second-type query.

Output

For every second-type query, print one integer in a new line — answer to this query.

Samples

Sample Input 1

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

Sample Output 1

4
1
2

Sample Input 2

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

Sample Output 2

3
5
4
2
3
3
3
1

Note

In the first example p={5,3,4,2,1}p = \{5, 3, 4, 2, 1\}.

The first query is to print p3p_3. The answer is 44.

The second query is to print pp1p_{p_1}. The answer is 11.

The third query is to swap p1p_1 and p3p_3. Now p={4,3,5,2,1}p = \{4, 3, 5, 2, 1\}.

The fourth query is to print pp1p_{p_1}. The answer is 22.