CF1619H.Permutation and Queries

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

输入输出样例

  • 输入#1

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

    输出#1

    4
    1
    2
  • 输入#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

    输出#2

    3
    5
    4
    2
    3
    3
    3
    1

说明/提示

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 .

首页