PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000052. Hoán vị và truy vấn (Permutation and Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hoán vị và truy vấn (Permutation and Queries)

Permutation and Queries

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a permutation pp of 1,…,n1,\ldots,n, process two operation types:

  • 1 x y: swap the values pxp_x and pyp_y, yielding a new permutation.
  • 2 i k: start with u=iu=i, repeat the assignment u←puu\leftarrow p_u exactly kk times, and print the resulting uu.

Every query uses the current permutation, not the original one.

Input

The first line contains n,qn,q, followed by the permutation. Each of the qq lines is 1 x y (1≤x≠y≤n1\le x\ne y\le n) or 2 i k (1≤i,k≤n1\le i,k\le n). At least one type-2 query is guaranteed.

Output

Print the final uu for each type-2 query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le60; 1≤q≤1001\le q\le100. The initial array is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le2000; 1≤q≤30001\le q\le3000. The initial array is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 3 (25 points): 1≤n≤250001\le n\le25000; 1≤q≤250001\le q\le25000. The initial array is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 4 (45 points): 1≤n≤1000001\le n\le100000; 1≤q≤1000001\le q\le100000. The initial array is a permutation of 1,2,…,n1,2,\ldots,n.

Examples

Input

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

Output

4
1
2

Explanation

Initially p=(5,3,4,2,1)p=(5,3,4,2,1). One step from 33 reaches 44; two steps from 11 reach 55 then 11. After swapping p1p_1 and p3p_3, the permutation is (4,3,5,2,1)(4,3,5,2,1); two steps from 11 go 1→4→21\to4\to2, giving 2.