A - ab

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字からなる長さ N の文字列 S が与えられます。
S の中で ab が隣接する箇所があれば Yes を、なければ No を出力してください。(ab の順序は問いません。)

制約

  • 2 \leq N \leq 100
  • S は英小文字からなる長さ N の文字列

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

S の中で ab が隣接する箇所があれば Yes を、なければ No を出力せよ。


入力例 1

3
abc

出力例 1

Yes

文字列 abc1 文字目にある a2 文字目にある b が隣接しています。よって Yes を出力してください。


入力例 2

2
ba

出力例 2

Yes

文字列 ba2 文字目にある a1 文字目にある b が隣接しています。(ab の順番は逆でも良い点に注意してください。)


入力例 3

7
atcoder

出力例 3

No

Score : 100 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
If there are any adjacent occurrences of a and b in S, print Yes; otherwise, print No. (The order of a and b does not matter.)

Constraints

  • 2 \leq N \leq 100
  • S is a string of length N consisting of lowercase English letters.

Input

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

N
S

Output

If there are any adjacent occurrences of a and b in S, print Yes; otherwise, print No.


Sample Input 1

3
abc

Sample Output 1

Yes

The string abc has a as the first character and b as the second character, which are adjacent. Thus, print Yes.


Sample Input 2

2
ba

Sample Output 2

Yes

The string ba has a as the second character and b as the first character, which are adjacent. (Note that the order of a and b does not matter.)


Sample Input 3

7
atcoder

Sample Output 3

No
B - aaaadaa

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の英小文字からなる文字列 S と英小文字 c_1,c_2 が与えられます。

S の文字のうち c_1 であるもの 以外 を全て c_2 に置き換えた文字列を求めてください。

制約

  • 1\le N\le 100
  • N は整数
  • c_1,c_2 は英小文字
  • S は英小文字からなる長さ N の文字列

入力

入力は以下の形式で標準入力から与えられる。

N c_1 c_2
S

出力

答えを出力せよ。


入力例 1

3 b g
abc

出力例 1

gbg

S= abc のうち、 b でない acg に置き換えた結果 gbg になります。したがって、 gbg を出力してください。


入力例 2

1 s h
s

出力例 2

s

置き換えた後の文字列が元の文字列と変わらない場合もあります。


入力例 3

7 d a
atcoder

出力例 3

aaaadaa

入力例 4

10 b a
acaabcabba

出力例 4

aaaabaabba

Score : 100 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, along with lowercase English letters c_1 and c_2.

Find the string obtained by replacing every character of S that is not c_1 with c_2.

Constraints

  • 1\le N\le 100
  • N is an integer.
  • c_1 and c_2 are lowercase English letters.
  • S is a string of length N consisting of lowercase English letters.

Input

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

N c_1 c_2
S

Output

Print the answer.


Sample Input 1

3 b g
abc

Sample Output 1

gbg

Replacing a and c (which are not b) with g in S= abc results in gbg, so print gbg.


Sample Input 2

1 s h
s

Sample Output 2

s

It is possible that the resulting string after replacement is the same as the original string.


Sample Input 3

7 d a
atcoder

Sample Output 3

aaaadaa

Sample Input 4

10 b a
acaabcabba

Sample Output 4

aaaabaabba
C - Who is Missing?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

長さ M の整数列 A=(A_1,A_2,\dots,A_M) が与えられます。
A の各要素は 1 以上 N 以下で、全ての要素は相異なります。

A の要素として含まれない 1 以上 N 以下の整数を、昇順に全て列挙してください。

制約

  • 入力は全て整数
  • 1 \le M \le N \le 1000
  • 1 \le A_i \le N
  • A の要素は相異なる

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 A_2 \dots A_M

出力

A の要素として含まれない 1 以上 N 以下の整数を昇順に全て挙げた列が (X_1,X_2,\dots,X_C) であるとき、以下の形式で出力せよ。

C
X_1 X_2 \dots X_C

入力例 1

10 3
3 9 2

出力例 1

7
1 4 5 6 7 8 10

A=(3,9,2) です。
A の要素として含まれない 1 以上 10 以下の整数を昇順に全て挙げると、 1,4,5,6,7,8,10 となります。


入力例 2

6 6
1 3 5 2 4 6

出力例 2

0

A の要素として含まれない 1 以上 6 以下の整数がひとつもありません。
この場合、 1 行目に 0 と出力し、 2 行目は空行としてください。


入力例 3

9 1
9

出力例 3

8
1 2 3 4 5 6 7 8

Score : 200 points

Problem Statement

You are given a sequence of M integers A = (A_1, A_2, \dots, A_M).
Each element of A is an integer between 1 and N, inclusive, and all elements are distinct.

List all integers between 1 and N that do not appear in A in ascending order.

Constraints

  • All input values are integers.
  • 1 \le M \le N \le 1000
  • 1 \le A_i \le N
  • The elements of A are distinct.

Input

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

N M
A_1 A_2 \dots A_M

Output

Let (X_1, X_2, \dots, X_C) be the sequence of all integers between 1 and N, inclusive, that do not appear in A, listed in ascending order. The output should be in the following format:

C
X_1 X_2 \dots X_C

Sample Input 1

10 3
3 9 2

Sample Output 1

7
1 4 5 6 7 8 10

Here, A=(3,9,2).
The integers between 1 and 10 that do not appear in A, listed in ascending order, are 1,4,5,6,7,8,10.


Sample Input 2

6 6
1 3 5 2 4 6

Sample Output 2

0

No integer between 1 and 6 is missing from A.
In this case, print 0 on the first line and leave the second line empty.


Sample Input 3

9 1
9

Sample Output 3

8
1 2 3 4 5 6 7 8
D - Spot the Difference

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

N マス、横 N マスのグリッドが 2 個与えられます。それぞれグリッド A, グリッド B と呼びます。
グリッドの各マスには英小文字が書かれています。
グリッド A の上から i 行目、左から j 列目に書かれている文字は A_{i, j} です。
グリッド B の上から i 行目、左から j 列目に書かれている文字は B_{i, j} です。

2 つのグリッドは 1 ヵ所だけ書かれている文字が異なります。すなわち、A_{i, j} \neq B_{i, j} である N 以下の正整数の組 (i, j) はちょうど 1 個存在します。この (i, j) を求めてください。

制約

  • 1 \leq N \leq 100
  • A_{i, j}, B_{i, j} は全て英小文字
  • A_{i, j} \neq B_{i, j} である (i, j) がちょうど 1 個存在する

入力

入力は以下の形式で標準入力から与えられる。

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

出力

A_{i, j} \neq B_{i, j} である N 以下の正整数の組を (i, j) とする。この時、(i, j) を以下の形式で出力せよ。

i j

入力例 1

3
abc
def
ghi
abc
bef
ghi

出力例 1

2 1

A_{2, 1} = dB_{2, 1} = b なので A_{2, 1} \neq B_{2, 1} が成り立つため、(i, j) = (2, 1) は問題文の条件を満たします。


入力例 2

1
f
q

出力例 2

1 1

入力例 3

10
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehfk
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehok
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj

出力例 3

5 9

Score: 150 points

Problem Statement

You are given two grids, each with N rows and N columns, referred to as grid A and grid B.
Each cell in the grids contains a lowercase English letter.
The character at the i-th row and j-th column of grid A is A_{i, j}.
The character at the i-th row and j-th column of grid B is B_{i, j}.

The two grids differ in exactly one cell. That is, there exists exactly one pair (i, j) of positive integers not greater than N such that A_{i, j} \neq B_{i, j}. Find this (i, j).

Constraints

  • 1 \leq N \leq 100
  • A_{i, j} and B_{i, j} are all lowercase English letters.
  • There exists exactly one pair (i, j) such that A_{i, j} \neq B_{i, j}.

Input

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

Output

Let (i, j) be the pair of positive integers not greater than N such that A_{i, j} \neq B_{i, j}. Print (i, j) in the following format:

i j

Sample Input 1

3
abc
def
ghi
abc
bef
ghi

Sample Output 1

2 1

From A_{2, 1} = d and B_{2, 1} = b, we have A_{2, 1} \neq B_{2, 1}, so (i, j) = (2, 1) satisfies the condition in the problem statement.


Sample Input 2

1
f
q

Sample Output 2

1 1

Sample Input 3

10
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehfk
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj
eixfumagit
vtophbepfe
pxbfgsqcug
ugpugtsxzq
bvfhxyehok
uqyfwtmglr
jaitenfqiq
acwvufpfvv
jhaddglpva
aacxsyqvoj

Sample Output 3

5 9
E - Rotate Colored Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字からなる長さ N の文字列 S が与えられます。 S の各文字は色 1 、色 2\ldots 、色 MM 色のうちのいずれかで塗られており、 i = 1, 2, \ldots, N について、Si 文字目は色 C_i で塗られています。

i = 1, 2, \ldots, M について、この順番に下記の操作を行います。

  • S の色 i で塗られた文字からなる部分を、右に 1 つ巡回シフトする。 すなわち、S の 色 i で塗られた文字の位置が先頭のものから順に p_1, p_2, p_3, \ldots, p_k 文字目であるとき、 Sp_1, p_2, p_3, \ldots, p_k 文字目を、それぞれ、Sp_k, p_1,p_2, \ldots, p_{k-1} 文字目で同時に置き換える。

上記の操作をすべて行った後の、最終的な S を出力してください。

なお、M 色あるどの色についても、その色で塗られた S の文字が必ず 1 つ以上存在することが、制約として保証されます。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, C_i はすべて整数
  • S は英小文字からなる長さ N の文字列
  • 任意の整数 1 \leq i \leq M に対して、ある整数 1 \leq j \leq N が存在して C_j = i が成り立つ

入力

入力は以下の形式で標準入力から与えられる。

N M
S
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

出力例 1

cszapqbr

はじめ、S = apzbqrcs です。

  • i = 1 に対する操作では、S1, 4, 7 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cpzaqrbs となります。
  • i = 2 に対する操作では、S2, 5, 6, 8 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります。
  • i = 3 に対する操作では、S3 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります(操作の前後で S は変わりません)。

よって、最終的な S である cszapqbr を出力します。


入力例 2

2 1
aa
1 1

出力例 2

aa

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters. Each character of S is painted in one of the M colors: color 1, color 2, ..., color M; for each i = 1, 2, \ldots, N, the i-th character of S is painted in color C_i.

For each i = 1, 2, \ldots, M in this order, let us perform the following operation.

  • Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters are painted in color i from left to right, then simultaneously replace the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters of S with the p_k-th, p_1-th, p_2-th, \ldots, p_{k-1}-th characters of S, respectively.

Print the final S after the above operations.

The constraints guarantee that at least one character of S is painted in each of the M colors.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, and C_i are all integers.
  • S is a string of length N consisting of lowercase English letters.
  • For each integer 1 \leq i \leq M, there is an integer 1 \leq j \leq N such that C_j = i.

Input

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

N M
S
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

Sample Output 1

cszapqbr

Initially, S = apzbqrcs.

  • For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th characters, resulting in S = cpzaqrbs.
  • For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th characters, resulting in S = cszapqbr.
  • For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting in S = cszapqbr (here, S is not changed).

Thus, you should print cszapqbr, the final S.


Sample Input 2

2 1
aa
1 1

Sample Output 2

aa
F - AtCoder AAC Contest

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋くんは、文字をいくつか持っています。 高橋くんが持っている文字は A, B, C のいずれかです。 はじめ、高橋くんは An _ A 個、Bn _ B 個、Cn _ C 個持っています。

高橋くんは、A1 つ、C1 つ、さらに追加で任意の 1 つの文字の合計 3 文字を使うことで、コンテストを 1 つ開催することができます。 具体的には、A2 つ、C1 つ使うと AAC を、A, B, C1 つずつ使うと ABC を、A1 つ、C2 つ使うと ACC を開催することができます。

高橋くんは、現在持っている文字を使ってコンテストをなるべく多く開催したいです。 高橋くんが最大でいくつのコンテストを開催することができるか求めてください。

T 個のテストケースが与えられるので、それぞれについて答えてください。

制約

  • 1\le T\le2\times10 ^ 5
  • それぞれのテストケースについて、
    • 0\le n _ A\le10 ^ 9
    • 0\le n _ B\le10 ^ 9
    • 0\le n _ C\le10 ^ 9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

\mathrm{testcase} _ i\ (1\le i\le T)i 番目のテストケースを表しており、次の形式で与えられる。

n _ A n _ B n _ C

出力

T 行にわたって出力せよ。 i 行目 (1\le i\le T) には、i 番目のテストケースの答えを出力せよ。


入力例 1

5
3 1 2
100 0 0
1000000 1000000 1000000
31 41 59
1000000000 10000 1

出力例 1

2
0
1000000
31
1

1 つめのテストケースでは、AAC1 回、ABC1 回の計 2 回のコンテストを開催することができます。 よって、1 行目には 2 を出力してください。

Score : 300 points

Problem Statement

Takahashi has some letters. Each letter he has is A, B, or C. Initially, he has n _ A letters A, n _ B letters B, and n _ C letters C.

He can hold one contest by using one letter A, one letter C, and additionally any one letter, for a total of three letters. Specifically, he can hold AAC by using two letters A and one letter C, ABC by using one letter each of A, B, and C, and ACC by using one letter A and two letters C.

He wants to hold as many contests as possible using the letters he currently has. Find the maximum number of contests he can hold.

T test cases are given, so report the answer for each of them.

Constraints

  • 1\le T\le2\times10 ^ 5
  • For each test case,
    • 0\le n _ A\le10 ^ 9
    • 0\le n _ B\le10 ^ 9
    • 0\le n _ C\le10 ^ 9
  • All input values are integers.

Input

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

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

\mathrm{testcase} _ i\ (1\le i\le T) represents the i-th test case and is given in the following format:

n _ A n _ B n _ C

Output

Output over T lines. On the i-th line (1\le i\le T), output the answer to the i-th test case.


Sample Input 1

5
3 1 2
100 0 0
1000000 1000000 1000000
31 41 59
1000000000 10000 1

Sample Output 1

2
0
1000000
31
1

In the first test case, he can hold AAC once and ABC once, for a total of two contests. Therefore, output 2 on the first line.

G - Make Geometric Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB



配点 : 425

問題文

長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。 ここで、どの i\ (1\le i\le N) についても A _ i0 でないことが保証されます。

A を適切に並べ替えた数列 B=(B _ 1,B _ 2,\ldots,B _ N) が等比数列になることがあるか判定してください。

ただし、数列 S=(S _ 1,S _ 2,\ldots,S _ N) が等比数列であるとは、ある実数 r が存在してすべての整数 1\le i\lt N に対して S _ {i+1}=rS _ i が成り立つことをいいます。

1 つの入力ファイルにつき、T 個のテストケースを解いてください。

制約

  • 1\le T\le10 ^ 5
  • 2\le N\le2\times10 ^ 5
  • -10 ^ 9\le A _ i\le10 ^ 9\ (1\le i\le N)
  • A _ i\ne0\ (1\le i\le N)
  • 1 つの入力ファイルにおける N の総和は 2\times10 ^ 5 以下
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

ここで、\mathrm{testcase} _ ii 番目 (1\le i\le T) のテストケースであり、各テストケースは以下の形式で与えられる。

N
A _ 1 A _ 2 \ldots A _ N

出力

T 行にわたって出力せよ。 i 行目 (1\le i\le T) には、i 番目のテストケースにおいて A を並べ替えて等比数列にできる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3
5
1 8 2 4 16
5
-16 24 54 81 -36
7
90000 8100 -27000 729 -300000 -2430 1000000

出力例 1

Yes
No
Yes

1 つめのテストケースでは、A を並べ替えた (16,8,4,2,1) は、公比 r=\dfrac12 の等比数列になります。 よって、1 行目には Yes と出力してください。

2 つめのテストケースでは、A をどのように並べ替えても条件を満たしません。 よって、2 行目には No と出力してください。

Score : 425 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N. It is guaranteed that for any i\ (1\le i\le N), A_i is not 0.

Determine whether there exists a permutation B=(B_1,B_2,\ldots,B_N) of A such that B forms a geometric sequence.

A sequence S=(S_1,S_2,\ldots,S_N) is a geometric sequence if there exists a real number r such that S_{i+1}=rS_i for all integers 1\le i\lt N.

Solve T test cases per input file.

Constraints

  • 1\le T\le10^5
  • 2\le N\le2\times10^5
  • -10^9\le A_i\le10^9\ (1\le i\le N)
  • A_i\ne0\ (1\le i\le N)
  • The sum of N over all test cases in a single input file is at most 2\times10^5.
  • All input values are integers.

Input

The input is given from standard input in the following format:

T
\mathrm{testcase}_1
\mathrm{testcase}_2
\vdots
\mathrm{testcase}_T

where \mathrm{testcase}_i is the i-th test case (1\le i\le T), and each test case is given in the following format:

N
A_1 A_2 \ldots A_N

Output

Output T lines. The i-th line (1\le i\le T) should contain Yes if A can be rearranged to form a geometric sequence in the i-th test case, and No otherwise.


Sample Input 1

3
5
1 8 2 4 16
5
-16 24 54 81 -36
7
90000 8100 -27000 729 -300000 -2430 1000000

Sample Output 1

Yes
No
Yes

In the first test case, the rearrangement (16,8,4,2,1) of A forms a geometric sequence with common ratio r=\frac{1}{2}. Thus, print Yes on the first line.

In the second test case, no rearrangement of A satisfies the condition. Thus, print No on the second line.

H - Geometric Progression

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^iM で割った余りを求めてください。

制約

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

A X M

出力

答えを出力せよ。


入力例 1

3 4 7

出力例 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40 です。407 で割った余りは 5 であるため、5 を出力します。


入力例 2

8 10 9

出力例 2

0

入力例 3

1000000000 1000000000000 998244353

出力例 3

919667211

Score : 500 points

Problem Statement

Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.

Constraints

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • All values in the input are integers.

Input

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

A X M

Output

Print the answer.


Sample Input 1

3 4 7

Sample Output 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.


Sample Input 2

8 10 9

Sample Output 2

0

Sample Input 3

1000000000 1000000000000 998244353

Sample Output 3

919667211
I - BOX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 個の箱 1,2,\dots,N と、 10^{100} 個のボール 1,2,\dots,10^{100} があります。 最初、箱 i にはボール i のみが入っています。

ここに以下の操作が合計 Q 回行われるので、処理してください。

操作にはタイプ 1,2,33 種類があります。

タイプ 1 : 箱 X に箱 Y の中身を全て入れる。 この操作では X \neq Y が保証される。

1 X Y

タイプ 2 : 現在いずれかの箱に入っているボールの数の合計を k とすると、箱 X にボール k+1 を入れる。

2 X

タイプ 3 : ボール X が入っている箱の番号を答える。

3 X

制約

  • 入力は全て整数
  • 2 \le N \le 3 \times 10^5
  • 1 \le Q \le 3 \times 10^5
  • タイプ 1 の操作について、 1 \le X,Y \le N かつ X \neq Y
  • タイプ 2 の操作について、 1 \le X \le N
  • タイプ 3 の操作について、その時点でボール X がいずれかの箱に入っている
  • タイプ 3 の操作が少なくとも 1 つ与えられる

入力

入力は以下の形式で標準入力から与えられる。
但し、 op_ii 回目の操作を表す。

N Q
op_1
op_2
\vdots
op_Q

出力

各タイプ 3 の操作に対して、答えを 1 行に 1 つ、整数として出力せよ。


入力例 1

5 10
3 5
1 1 4
2 1
2 4
3 7
1 3 1
3 4
1 1 4
3 7
3 6

出力例 1

5
4
3
1
3

この入力は 10 個の操作を含みます。

  • 1 回目の操作はタイプ 3 です。ボール 5 は箱 5 に入っています。
  • 2 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
    • 1 の中身はボール 1,4 、箱 4 の中身は空になります。
  • 3 回目の操作はタイプ 2 です。箱 1 にボール 6 を入れます。
  • 4 回目の操作はタイプ 2 です。箱 4 にボール 7 を入れます。
  • 5 回目の操作はタイプ 3 です。ボール 7 は箱 4 に入っています。
  • 6 回目の操作はタイプ 1 です。箱 3 に箱 1 の中身を全て入れます。
    • 3 の中身はボール 1,3,4,6 、箱 1 の中身は空になります。
  • 7 回目の操作はタイプ 3 です。ボール 4 は箱 3 に入っています。
  • 8 回目の操作はタイプ 1 です。箱 1 に箱 4 の中身を全て入れます。
    • 1 の中身はボール 7 、箱 4 の中身は空になります。
  • 9 回目の操作はタイプ 3 です。ボール 7 は箱 1 に入っています。
  • 10 回目の操作はタイプ 3 です。ボール 6 は箱 3 に入っています。

Score : 500 points

Problem Statement

There are N boxes numbered 1,2,\ldots,N, and 10^{100} balls numbered 1,2,\dots,10^{100}. Initially, box i contains just ball i.

Process a total of Q operations that will be performed.

There are three types of operations: 1, 2, and 3.

Type 1: Put all contents of box Y into box X. It is guaranteed that X \neq Y.

1 X Y

Type 2: Put ball k+1 into box X, where k is the current total number of balls contained in the boxes.

2 X

Type 3: Report the number of the box that contains ball X.

3 X

Constraints

  • All values in the input are integers.
  • 2 \le N \le 3 \times 10^5
  • 1 \le Q \le 3 \times 10^5
  • For each type-1 operation, 1 \le X,Y \le N and X \neq Y.
  • For each type-2 operation, 1 \le X \le N.
  • For each type-3 operation, ball X is contained in some box at that point.
  • There is at least one type-3 operation.

Input

The input is given from Standard Input in the following format.
Here, op_i represents the i-th operation.

N Q
op_1
op_2
\vdots
op_Q

Output

For each type-3 operation, print a line containing the response as an integer.


Sample Input 1

5 10
3 5
1 1 4
2 1
2 4
3 7
1 3 1
3 4
1 1 4
3 7
3 6

Sample Output 1

5
4
3
1
3

This input contains ten operations.

  • The first operation is of type 3. Ball 5 is in box 5.
  • The second operation is of type 1. Put all contents of box 4 into box 1.
    • Box 1 now contains balls 1 and 4, and box 4 is now empty.
  • The third operation is of type 2. Put ball 6 into box 1.
  • The fourth operation is of type 2. Put ball 7 into box 4.
  • The fifth operation is of type 3. Ball 7 is in box 4.
  • The sixth operation is of type 1. Put all contents of box 1 into box 3.
    • Box 3 now contains balls 1, 3, 4, and 6, and box 1 is now empty.
  • The seventh operation is of type 3. Ball 4 is in box 3.
  • The eighth operation is of type 1. Put all contents of box 4 into box 1.
    • Box 1 now contains ball 7, and box 4 is now empty.
  • The ninth operation is of type 3. Ball 7 is in box 1.
  • The tenth operation is of type 3. Ball 6 is in box 3.