CF1619H.Permutation and Queries

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

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.

给你一个 nn 个元素的排列 pp。nn 个元素的排列是指一个长度为 nn 的数组,其中恰好包含从 11 到 nn 的每个整数各一次。例如,[1,2,3][1, 2, 3] 和 [4,3,5,1,2][4, 3, 5, 1, 2] 是排列,但 [1,2,4][1, 2, 4] 和 [4,3,2,1,2][4, 3, 2, 1, 2] 不是排列。你需要执行 qq 个查询。

查询分为两类:

  • 11 xx yy — 交换 pxp_x 和 pyp_y。
  • 22 ii kk — 输出将 ii 执行 kk 次赋值 i=pii = p_i 后所得的数值。

输入格式

The first line contains two integers nn and qq (1≤n,q≤1051 \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 (1≤t≤21 \le t \le 2) — type of query. If t=1t = 1, then the next two integers are xx and yy (1≤x,y≤n1 \le x, y \le n; x≠yx \ne y) — first-type query. If t=2t = 2, then the next two integers are ii and kk (1≤i,k≤n1 \le i, k \le n) — second-type query.

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

第一行包含两个整数 nn 和 qq(1≤n,q≤1051 \le n, q \le 10^5)。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n。

接下来的 qq 行,每行包含三个整数。第一个整数为 tt(1≤t≤21 \le t \le 2),表示查询类型。若 t=1t = 1,则接下来的两个整数为 xx 和 yy(1≤x,y≤n1 \le x, y \le n;x≠yx \ne y),表示第一类查询。若 t=2t = 2,则接下来的两个整数为 ii 和 kk(1≤i,k≤n1 \le i, k \le n),表示第二类查询。

保证至少存在一个第二类查询。

输出格式

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,1p = {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,1p = {4, 3, 5, 2, 1}.

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

在第一个例子中,p=5,3,4,2,1p = {5, 3, 4, 2, 1}。

第一个查询要求输出 p3p_3,答案为 44。

第二个查询要求输出 pp1p_{p_1},答案为 11。

第三个查询要求交换 p1p_1 和 p3p_3。此时 p=4,3,5,2,1p = {4, 3, 5, 2, 1}。

第四个查询要求输出 pp1p_{p_1},答案为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页