JPRSプログラミングコンテスト2026#2 (AtCoder Beginner Contest 470)
D
Inverse and Swap
AtCoder
⌀ 777
原题
← CE →

Inverse and SwapAI (CI翻译)

Problem Statement

You are given a permutation $P = (P_1, \dots, P_N)$ of $(1, \dots, N)$.

Process $Q$ queries in order. There are two types of queries as follows:

  • 1 x y: Swap the values of $P_x$ and $P_y$.
  • 2: Construct the permutation $P' = (P'_1, \dots, P'_N)$ of $(1, \dots, N)$ satisfying the following condition, and replace the values of $P_1, \dots, P_N$ with $P'_1, \dots, P'_N$, respectively. (One can prove that such $P'$ uniquely exists.)
  • $P_{P'_i} = i$ for every integer $i$ satisfying $1 \leq i \leq N$.

Output the values of $P_1, \dots, P_N$ after processing all queries.

Constraints

  • $2 \leq N \leq 5 \times 10^5$
  • $1 \leq Q \leq 5 \times 10^5$
  • $(P_1, \dots, P_N)$ is a permutation of $(1, \dots, N)$.
  • $1 \leq x < y \leq N$ for queries of type $1$.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
P_1 P_2 \cdots P_N
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Here, $\mathrm{query}_q$ represents the $q$-th query, and is given in one of the following two formats:

1 x y

2

Output

Output the values of $P_1, \dots, P_N$ after processing all queries, separated by spaces, on one line.

{"titleZh":"逆置换与交换","bodyZh":"## 题目描述\n\n给定一个 $(1, \dots, N)$ 的排列 $P = (P_1, \dots, P_N)$。\n\n请按顺序处理 $Q$ 个查询。查询有以下两种类型:\n\n- 1 x y:交换 $P_x$ 和 $P_y$ 的值。\n- 2:构造一个满足以下条件的 $(1, \dots, N)$ 的排列 $P' = (P'_1, \dots, P'_N)$,并将 $P_1, \dots, P_N$ 的值分别替换为 $P'_1, \dots, P'_N$。(可以证明,这样的 $P'$ 唯一存在。)\n- 对于满足 $1 \leq i \leq N$ 的每个整数 $i$,都有 $P_{P'_i} = i$。\n\n输出处理完所有查询后 $P_1, \dots, P_N$ 的值。\n\n## 约束条件\n\n- $2 \leq N \leq 5 \times 10^5$\n- $1 \leq Q \leq 5 \times 10^5$\n- $(P_1, \dots, P_N)$ 是 $(1, \dots, N)$ 的排列。\n- 对于类型为 $1$ 的查询,满足 $1 \leq x < y \leq N$。\n- 所有输入值均为整数。\n\n## 输入格式\n\n输入从标准输入按以下格式给出:\n\n\nN Q\nP_1 P_2 \\cdots P_N\n\\mathrm{query}_1\n\\vdots\n\\mathrm{query}_Q\n`\n\n其中,$\\mathrm{query}_q$ 表示第 $q$ 个查询,格式如下二者之一:\n\n`\n1 x y\n`\n\n`\n2\n\n\n## 输出格式\n\n输出处理完所有查询后 $P_1, \dots, P_N$ 的值,数字之间用空格分隔,输出一行。"}AI (CI翻译)

样例

Input #1
5 5
2 1 3 5 4
1 2 4
2
1 2 3
1 3 4
2
Output #1
4 5 2 1 3
At the point when each query has been processed, the values of $P_1, \dots, P_N$ are as follows: - After processing the first query, $P = (2,5,3,1,4)$. - After processing the second query, $P = (4,1,3,5,2)$. - After processing the third query, $P = (4,3,1,5,2)$. - After processing the fourth query, $P = (4,3,5,1,2)$. - After processing the fifth query, $P = (4,5,2,1,3)$.
Input #2
7 4
3 7 5 6 4 2 1
2
2
2
2
Output #2
3 7 5 6 4 2 1
Input #3
10 8
7 3 2 4 8 5 10 9 1 6
2
1 4 10
1 6 9
2
1 9 10
1 3 10
2
1 4 6
Output #3
3 10 2 8 6 7 1 5 9 4
题目信息
题号
abc470_d
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。