A - Contest Result

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 問の問題からなるコンテストが開催され、i\ (1\leq i\leq N) 問目の配点は A_i 点でした。

すぬけくんはこのコンテストに参加し、B_1,B_2,\ldots,B_M 問目の M 問を解きました。 すぬけくんの総得点を求めてください。

ただし、総得点とは解いた問題の配点の総和を意味するものとします。

制約

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq 100
  • 1\leq B_1 < B_2 < \ldots < B_M \leq N
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

答えを整数として出力せよ。


入力例 1

3 2
10 20 30
1 3

出力例 1

40

すぬけくんは 1 問目と 3 問目を解きました。 配点はそれぞれ 10 点と 30 点なので、総得点は 10+30=40 点です。


入力例 2

4 1
1 1 1 100
4

出力例 2

100

入力例 3

8 4
22 75 26 45 72 81 47 29
4 6 7 8

出力例 3

202

Score : 100 points

Problem Statement

There was a contest with N problems. The i-th (1\leq i\leq N) problem was worth A_i points.

Snuke took part in this contest and solved M problems: the B_1-th, B_2-th, \ldots, and B_M-th ones. Find his total score.

Here, the total score is defined as the sum of the points for the problems he solved.

Constraints

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq 100
  • 1\leq B_1 < B_2 < \ldots < B_M \leq N
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print the answer as an integer.


Sample Input 1

3 2
10 20 30
1 3

Sample Output 1

40

Snuke solved the 1-st and 3-rd problems, which are worth 10 and 30 points, respectively. Thus, the total score is 10+30=40 points.


Sample Input 2

4 1
1 1 1 100
4

Sample Output 2

100

Sample Input 3

8 4
22 75 26 45 72 81 47 29
4 6 7 8

Sample Output 3

202
B - Edge Checker

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

下の画像で示す図において、a 番の点と b 番の点が線で直接結ばれているかを答えてください。

制約

  • 1 \leq a \lt b \leq 10
  • a, b は整数

入力

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

a b

出力

a 番の点と b 番の点が線で直接結ばれている場合は Yes を出力し、結ばれていない場合は No を出力せよ。
ジャッジは英大文字と英小文字を厳密に区別することに注意せよ。


入力例 1

4 5

出力例 1

Yes

問題文で示した図において、4 番の点と 5 番の点は線で直接結ばれています。 よって、Yes を出力します。


入力例 2

3 5

出力例 2

No

問題文で示した図において、3 番の点と 5 番の点は線で直接結ばれていません。 よって、No を出力します。


入力例 3

1 10

出力例 3

Yes

Score : 100 points

Problem Statement

In the figure shown in the image below, are the points numbered a and b directly connected by a line segment?

Constraints

  • 1 \leq a \lt b \leq 10
  • a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the points numbered a and b are directly connected by a line segment, print Yes; otherwise, print No.
The judge is case-sensitive: be sure to print uppercase and lowercase letters correctly.


Sample Input 1

4 5

Sample Output 1

Yes

In the figure shown in the Problem Statement, the points numbered 4 and 5 are directly connected by a line segment.
Thus, Yes should be printed.


Sample Input 2

3 5

Sample Output 2

No

In the figure shown in the Problem Statement, the points numbered 3 and 5 are not directly connected by a line segment.
Thus, No should be printed.


Sample Input 3

1 10

Sample Output 3

Yes
C - ABCDEFG

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

直線上に 7 個の点 A, B, C, D, E, F, G がこの順に並んでいます。(下の図も参考にしてください)
隣り合う点の距離は次の通りです。

  • A と点 B の距離は 3
  • B と点 C の距離は 1
  • C と点 D の距離は 4
  • D と点 E の距離は 1
  • E と点 F の距離は 5
  • F と点 G の距離は 9

image

2 つの英大文字 p, q が与えられます。p, qA,B,C,D,E,F,G のいずれかで、 p \neq q が成り立ちます。
p と点 q の間の距離を答えてください。

制約

  • p, qA,B,C,D,E,F,G のいずれか
  • p \neq q

入力

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

p q

出力

p と点 q の間の距離を出力せよ。


入力例 1

A C

出力例 1

4

A と点 C の距離は 3 + 1 = 4 です。


入力例 2

G B

出力例 2

20

G と点 B の距離は 9 + 5 + 1 + 4 + 1 = 20 です。


入力例 3

C F

出力例 3

10

Score : 200 points

Problem Statement

There are 7 points A, B, C, D, E, F, and G on a straight line, in this order. (See also the figure below.)
The distances between adjacent points are as follows.

  • Between A and B: 3
  • Between B and C: 1
  • Between C and D: 4
  • Between D and E: 1
  • Between E and F: 5
  • Between F and G: 9

image

You are given two uppercase English letters p and q. Each of p and q is A, B, C, D, E, F, or G, and it holds that p \neq q.
Find the distance between the points p and q.

Constraints

  • Each of p and q is A,B,C,D,E,F, or G.
  • p \neq q

Input

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

p q

Output

Print the distance between the points p and q.


Sample Input 1

A C

Sample Output 1

4

The distance between the points A and C is 3 + 1 = 4.


Sample Input 2

G B

Sample Output 2

20

The distance between the points G and B is 9 + 5 + 1 + 4 + 1 = 20.


Sample Input 3

C F

Sample Output 3

10
D - Unauthorized

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

ある日、高橋くんはあるウェブサイトに対して N 回の操作を行いました。

i 回目 (1\leq i\leq N) の操作は文字列 S _ i で表され、次の 4 つのうちいずれかです。

  • S _ i= login である。高橋くんはログイン操作を行い、高橋くんがウェブサイトにログインした状態になる。
  • S _ i= logout である。高橋くんはログアウト操作を行い、高橋くんがウェブサイトにログインしていない状態になる。
  • S _ i= public である。高橋くんはウェブサイトの公開ページにアクセスする。
  • S _ i= private である。高橋くんはウェブサイトの非公開ページにアクセスする。

高橋くんがログインしていない状態で非公開ページにアクセスした時、またその時に限り、ウェブサイトは認証エラーを返します。

ログインした状態でさらにログイン操作をしたり、ログインしていない状態でさらにログアウト操作をしてもエラーにはなりません。 また、認証エラーが返されたあとも、高橋くんは操作を続けることができます。

はじめ、高橋くんはログインしていない状態です。

N 回の操作のうち、高橋くんが認証エラーを受け取った回数を出力してください。

制約

  • 1\leq N\leq100
  • N は整数
  • S _ ilogin, logout, public, private のいずれか (1\leq i\leq N)

入力

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

N
S _ 1
S _ 2
\vdots
S _ N

出力

高橋くんが認証エラーを受け取った回数を出力せよ。


入力例 1

6
login
private
public
logout
private
public

出力例 1

1

高橋くんが行うそれぞれの操作の結果は以下のようになります。

  • 高橋くんがウェブサイトにログインした状態になる。
  • 高橋くんは非公開ページにアクセスする。高橋くんは現在ログインしているので、エラーは返されない。
  • 高橋くんは公開ページにアクセスする。
  • 高橋くんがウェブサイトにログインしていない状態になる。
  • 高橋くんは非公開ページにアクセスする。高橋くんは現在ログインしていないので、認証エラーが返される。
  • 高橋くんは公開ページにアクセスする。

高橋くんが認証エラーを受け取るのは 5 回目の操作のみなので、1 を出力してください。


入力例 2

4
private
private
private
logout

出力例 2

3

連続で非公開ページにアクセスしようとした場合、操作のたびに認証エラーを受け取ります。

ログインしていない状態からさらにログアウト操作をした場合には認証エラーは返されないことに注意してください。


入力例 3

20
private
login
private
logout
public
logout
logout
logout
logout
private
login
login
private
login
private
login
public
private
logout
private

出力例 3

3

Score : 200 points

Problem Statement

One day, Takahashi performed N operations on a certain web site.

The i‑th operation (1 \le i \le N) is represented by a string S_i, which is one of the following:

  • S _ i= login: He performs a login operation and becomes logged in to the site.
  • S _ i= logout: He performs a logout operation and becomes not logged in to the site.
  • S _ i= public: He accesses a public page of the site.
  • S _ i= private: He accesses a private page of the site.

The site returns an authentication error if and only if he accesses a private page while he is not logged in.

Logging in again while already logged in, or logging out again while already logged out, does not cause an error. Even after an authentication error is returned, he continues performing the remaining operations.

Initially, he is not logged in.

Print the number of operations among the N operations at which he receives an authentication error.

Constraints

  • 1 \le N \le 100
  • N is an integer.
  • Each S_i is one of login, logout, public, private. (1 \le i \le N)

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the number of times Takahashi receives an authentication error.


Sample Input 1

6
login
private
public
logout
private
public

Sample Output 1

1

The result of each operation is as follows:

  • Takahashi becomes logged in.
  • He accesses a private page. He is logged in, so no error is returned.
  • He accesses a public page.
  • He becomes logged out.
  • He accesses a private page. He is not logged in, so an authentication error is returned.
  • He accesses a public page.

An authentication error occurs only at the 5th operation, so print 1.


Sample Input 2

4
private
private
private
logout

Sample Output 2

3

If he tries to access private pages consecutively while not logged in, he receives an authentication error for each such operation.

Note that logging out again while already logged out does not cause an authentication error.


Sample Input 3

20
private
login
private
logout
public
logout
logout
logout
logout
private
login
login
private
login
private
login
public
private
logout
private

Sample Output 3

3
E - Happy New Year!

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

10 進法で表記したときに 0,2 のみからなる正整数のうち、 K 番目に小さいものを求めてください。

制約

  • K1 以上 10^{18} 以下の整数

入力

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

K

出力

答えを整数として出力せよ。
ただし、たとえ答えが大きな整数であっても、求める答えを正確に整数として出力する必要がある。たとえば、 2.34e+22 のような指数表記や、 0523 のような先頭に不要な 0 を付けたような表記は許されない。


入力例 1

3

出力例 1

22

10 進法で表記した時に 0,2 のみからなる正整数を小さい方から並べると、 2,20,22,\dots となります。
このうち K=3 番目である 22 を出力してください。


入力例 2

11

出力例 2

2022

入力例 3

923423423420220108

出力例 3

220022020000202020002022022000002020002222002200002022002200

たとえ答えが大きな整数であっても、求める答えを正確に整数として出力する必要があることに注意してください。

Score : 300 points

Problem Statement

Among the positive integers that consist of 0's and 2's when written in base 10, find the K-th smallest integer.

Constraints

  • K is an integer between 1 and 10^{18} (inclusive).

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer as an integer.
Here, the exact value must be printed as an integer, even if it is big. Exponential notations such as 2.34e+22, for example, or unnecessary leading zeros such as 0523 are not allowed.


Sample Input 1

3

Sample Output 1

22

The positive integers that consist of 0's and 2's when written in base 10 are 2,20,22,\dots in ascending order.
The (K=) 3-rd of them, which is 22, should be printed.


Sample Input 2

11

Sample Output 2

2022

Sample Input 3

923423423420220108

Sample Output 3

220022020000202020002022022000002020002222002200002022002200

Note that the exact value of the answer must be printed as an integer, even if it is big.

F - Not Adjacent

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

a, b, c からなる文字列 S が与えられます。

S の空でない部分文字列であって、同じ文字が隣り合わないものの個数を 998244353 で割った余りを求めてください。

ただし、 2 つの部分文字列が文字列として一致しても、取り出す位置が異なるならば区別するものとします。

部分文字列とは S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • Sa, b, c からなる長さ 1 以上 3 \times 10^5 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

abbc

出力例 1

6

同じ文字が隣合わない部分文字列は以下の 6 個です。

  • a ( S1 文字目から 1 文字目まで)
  • b ( S2 文字目から 2 文字目まで)
  • b ( S3 文字目から 3 文字目まで)
  • c ( S4 文字目から 4 文字目まで)
  • ab ( S1 文字目から 2 文字目まで)
  • bc ( S3 文字目から 4 文字目まで)

2 番目と 3 番目のもののように、文字列として一致しても、取り出す位置が異なるならば区別することに注意してください。


入力例 2

cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca

出力例 2

760

Score : 300 points

Problem Statement

You are given a string S consisting of a, b, c.

Find the number of non-empty substrings of S in which no two adjacent characters are the same, modulo 998244353.

Two substrings are considered distinct if they are taken from different positions, even if they are identical as strings.

What is a substring? A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • S is a string of length between 1 and 3 \times 10^5, inclusive, consisting of a, b, c.

Input

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

S

Output

Output the answer.


Sample Input 1

abbc

Sample Output 1

6

The substrings in which no two adjacent characters are the same are the following six:

  • a (from the 1st to the 1st character of S)
  • b (from the 2nd to the 2nd character of S)
  • b (from the 3rd to the 3rd character of S)
  • c (from the 4th to the 4th character of S)
  • ab (from the 1st to the 2nd character of S)
  • bc (from the 3rd to the 4th character of S)

Note that, as with the 2nd and 3rd entries, two substrings are considered distinct if they are taken from different positions, even if they are identical as strings.


Sample Input 2

cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca

Sample Output 2

760
G - Toggle Maze

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマス目を (i,j) と表します。各マスの状態は文字 A_{i,j} で表され、意味は以下の通りです。

  • . :空きマス。
  • # :障害物マス。
  • S :スタートマス。
  • G :ゴールマス。
  • o :開いたドアのマス。
  • x :閉じたドアのマス。
  • ? :スイッチマス。

高橋君は、 1 回の操作で今いるマスから上下左右に隣り合う、障害物マスでも閉じたドアでもないマスへ移動することができます。

また、スイッチマスに移動する度に全ての開いたドアのマスは閉じたドアのマスに、閉じたドアのマスは開いたドアのマスに変わります。

高橋君がはじめスタートマスにいる状態からゴールマスにいる状態にするよう操作できるか判定し、可能な場合は必要な操作回数の最小値を求めてください。

制約

  • 1\le H,W \le 500
  • H,W は整数
  • A_{i,j}., #, S, G, o, x, ? のいずれか
  • SGA_{i,j} にそれぞれちょうど 1 つずつ存在する

入力

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

H W
A_{1,1}A_{1,2}\cdots A_{1,W}
A_{2,1}A_{2,2}\cdots A_{2,W}
\vdots
A_{H,1}A_{H,2}\cdots A_{H,W}

出力

高橋君がはじめスタートマスにいる状態からゴールマスにいる状態するよう操作できる場合は必要な操作回数の最小値を、できない場合は -1 を出力せよ。


入力例 1

2 4
S.xG
#?o.

出力例 1

5

(1,1) から順に (1,2),(2,2),(1,2),(1,3),(1,4) と移動するよう操作することで 5 回の操作でゴールマスにいる状態にすることができます。


入力例 2

1 5
So?oG

出力例 2

-1

どのように操作してもゴールマスにいる状態にすることはできません。したがって、 -1 を出力してください。


入力例 3

5 5
Sx?x?
o#o#x
?o?o?
x#x#o
?x?oG

出力例 3

10

Score : 400 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. The state of each cell is represented by a character A_{i,j}, with the following meanings:

  • . : Empty cell.
  • # : Obstacle cell.
  • S : Start cell.
  • G : Goal cell.
  • o : Open door cell.
  • x : Closed door cell.
  • ? : Switch cell.

Takahashi can move from his current cell to an adjacent cell (up, down, left, right) that is neither an obstacle cell nor a closed door cell in one operation.

Also, every time he moves to a switch cell, all open door cells become closed door cells, and all closed door cells become open door cells.

Determine whether he can move from the initial state of being at the start cell to being at the goal cell, and if possible, find the minimum number of operations required.

Constraints

  • 1\le H,W \le 500
  • H and W are integers.
  • Each A_{i,j} is one of ., #, S, G, o, x, ?.
  • S and G each appear exactly once in A_{i,j}.

Input

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

H W
A_{1,1}A_{1,2}\cdots A_{1,W}
A_{2,1}A_{2,2}\cdots A_{2,W}
\vdots
A_{H,1}A_{H,2}\cdots A_{H,W}

Output

If Takahashi can move from the initial state of being at the start cell to being at the goal cell, output the minimum number of operations required; otherwise, output -1.


Sample Input 1

2 4
S.xG
#?o.

Sample Output 1

5

By moving from (1,1) to (1,2),(2,2),(1,2),(1,3),(1,4) in that order, he can reach the goal cell in five operations.


Sample Input 2

1 5
So?oG

Sample Output 2

-1

No matter how he operates, he cannot reach the goal cell. Therefore, output -1.


Sample Input 3

5 5
Sx?x?
o#o#x
?o?o?
x#x#o
?x?oG

Sample Output 3

10
H - Minimum Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

(1,2,\ldots,N) を並べ替えた整数列 P=(P _ 1,P _ 2,\ldots,P _ N) が与えられます。 ここで、P(1,2,\ldots,N) と等しくないことが保証されます。

あなたは、次の操作を 0 回以上行って P を列 (1,2,\ldots,N) と一致させたいです。

  • 1\le i\lt j\le N を満たす整数の組 (i,j) をひとつ選ぶ。P _ iP _ j の値を入れ替える。

P を列 (1,2,\ldots,N) と一致させるために必要な最小の操作回数を K 回とします。

K 回の操作で P を列 (1,2,\ldots,N) と一致させるような操作列の \mathbf{1} 回目の操作としてあり得る操作の数を求めてください。 ただし、2 つの操作は選んだ整数の組 (i,j) が異なるとき、またそのときに限り区別します。

制約

  • 2\le N\le3\times10 ^ 5
  • 1\le P _ i\le N\ (1\le i\le N)
  • P _ i\ne P _ j\ (1\le i\lt j\le N)
  • i\ne P _ i を満たす 1\le i\le N が存在する
  • 入力はすべて整数

入力

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

N
P _ 1 P _ 2 \ldots P _ N

出力

答えを出力せよ。


入力例 1

5
3 1 4 2 5

出力例 1

6

例えば、次のようにして 3 回の操作で目標を達成することができます。

  • (i,j)=(1,2) を選ぶ。P=(1,3,4,2,5) となる。
  • (i,j)=(2,4) を選ぶ。P=(1,2,4,3,5) となる。
  • (i,j)=(3,4) を選ぶ。P=(1,2,3,4,5) となる。

2 回以下の操作で目標を達成することはできないので、K=3 です。

上の説明の通り、最初の操作で (1,2) を選ぶと 3 回の操作で目標を達成できます。 その他にも、最初の操作で (1,3),(1,4),(2,3),(2,4),(3,4) のいずれかを選んだ場合にはそのあとの 2 回の操作を適切に行うことで P=(1,2,3,4,5) とすることができます。

よって、6 を出力してください。


入力例 2

2
2 1

出力例 2

1

入力例 3

20
15 5 13 17 9 11 20 4 14 16 6 3 8 19 12 7 10 18 2 1

出力例 3

77

Score : 475 points

Problem Statement

You are given an integer sequence P=(P _ 1,P _ 2,\ldots,P _ N) that is a permutation of (1,2,\ldots,N). Here, it is guaranteed that P is not equal to (1,2,\ldots,N).

You want to perform the following operation zero or more times to make P match the sequence (1,2,\ldots,N):

  • Choose a pair of integers (i,j) satisfying 1\le i\lt j\le N. Swap the values of P _ i and P _ j.

Let K be the minimum number of operations required to make P match the sequence (1,2,\ldots,N).

Find the number of operations that can be the first operation in a sequence of operations that makes P match the sequence (1,2,\ldots,N) in K operations. Two operations are distinguished if and only if the chosen pairs of integers (i,j) are different.

Constraints

  • 2\le N\le3\times10 ^ 5
  • 1\le P _ i\le N\ (1\le i\le N)
  • P _ i\ne P _ j\ (1\le i\lt j\le N)
  • There exists 1\le i\le N such that i\ne P _ i.
  • All input values are integers.

Input

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

N
P _ 1 P _ 2 \ldots P _ N

Output

Print the answer.


Sample Input 1

5
3 1 4 2 5

Sample Output 1

6

For example, the goal can be achieved in three operations as follows:

  • Choose (i,j)=(1,2). P becomes (1,3,4,2,5).
  • Choose (i,j)=(2,4). P becomes (1,2,4,3,5).
  • Choose (i,j)=(3,4). P becomes (1,2,3,4,5).

The goal cannot be achieved in two or fewer operations, so K=3.

As explained above, choosing (1,2) for the first operation allows the goal to be achieved in three operations. Additionally, if one of (1,3),(1,4),(2,3),(2,4),(3,4) is chosen for the first operation, then by performing the next two operations appropriately, P can be made equal to (1,2,3,4,5).

Therefore, print 6.


Sample Input 2

2
2 1

Sample Output 2

1

Sample Input 3

20
15 5 13 17 9 11 20 4 14 16 6 3 8 19 12 7 10 18 2 1

Sample Output 3

77
I - Erase between X and Y

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

数列 A があります。 はじめ、A=(0) です。 (すなわち、A0 を唯一の要素として含む長さ 1 の数列です)。

クエリが Q 個与えられるので、順に処理してください。 i 番目 (1\leq i\leq Q) のクエリは以下のいずれかの形式です。

  • 1 xA の中で x が現れる場所の直後に i を挿入する。
    • 具体的には、現在の Aj 番目の要素を A_jA の長さを n としたとき、A_p=x なる p に対して A(A_1,\dots,A_p,i,A_{p+1},\dots,A_n) で更新する。 ここで、このクエリを処理する直前の時点で A には x が含まれていることが保証される。
  • 2 x yA の中で xy の間にある要素の値の合計を出力し、それらの要素を全て削除する。
    • 具体的には、現在の Aj 番目の要素を A_jA の長さを n としたとき、A_p=x,A_q=y なる p,q に対して、A_{\min(p,q)+1} + \dots + A_{\max(p,q)-1} を出力し、 A(A_1,\dots,A_{\min(p,q)},A_{\max(p,q)},\dots,A_n) で更新する。 ここで、このクエリを処理する直前の時点で A には x および y が共に含まれていることが保証される。

なお、どのようなクエリの列に対しても、クエリを処理する過程で A の中に同じ値が複数回現れることはなく、ゆえに A の中である値が現れる場所は(存在するならば)一意であることに注意してください。

制約

  • 1\leq Q \leq 5\times 10^5
  • i 番目のクエリについて、
    • 1 種類目のクエリのとき:
      • 0\leq x < i
      • クエリを処理する直前の時点で A には x が含まれる
    • 2 種類目のクエリのとき:
      • 0\leq x < y < i
      • クエリを処理する直前の時点で A には x,y が共に含まれる
  • 入力は全て整数

入力

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

Q
\text{query}_{1}
\text{query}_{2}
\vdots
\text{query}_{Q}

ここで \text{query}_{i}i 番目のクエリを表し、以下のいずれかの形式で与えられる。

1 x
2 x y

出力

2 種類目のクエリの個数を q 個として、q 行出力せよ。 i 行目には、2 種類目のクエリのうち i 番目のものにおいて出力すべき値を出力せよ。


入力例 1

6
1 0
1 1
1 0
2 2 3
1 2
2 0 5

出力例 1

1
5

最初、A=(0) です。

  • 1 番目のクエリ:0 の直後に 1 を挿入します。A=(0,1) になります。
  • 2 番目のクエリ:1 の直後に 2 を挿入します。A=(0,1,2) になります。
  • 3 番目のクエリ:0 の直後に 3 を挿入します。A=(0,3,1,2) になります。
  • 4 番目のクエリ:23 の間にある要素、すなわち 1 を削除し、削除した値の合計である 1 を出力します。A=(0,3,2) になります。
  • 5 番目のクエリ:2 の直後に 5 を挿入します。A=(0,3,2,5) になります。
  • 6 番目のクエリ:05 の間にある要素、すなわち 3,2 を削除し、削除した値の合計である 5 を出力します。A=(0,5) になります。

入力例 2

2
1 0
2 0 1

出力例 2

0

2 番目のクエリでは 01 の間にある要素を全て削除しますが、実際にはそのような要素は一つも存在せず、要素の削除も行われないため、出力する値は 0 になります。


入力例 3

10
1 0
1 1
2 0 2
2 0 2
1 0
1 5
2 0 5
2 2 6
1 6
1 9

出力例 3

1
0
0
0

Score : 525 points

Problem Statement

There is a sequence A. Initially, A=(0). (That is, A is a sequence of length 1 containing 0 as its only element).

You are given Q queries to process in order. The i-th query (1\leq i\leq Q) has one of the following forms:

  • 1 x: Insert i immediately after the location where x appears in A. Specifically, let A_j be the j-th element of the current A and n be the length of A. For p such that A_p=x, update A to (A_1,\dots,A_p,i,A_{p+1},\dots,A_n). It is guaranteed that A contains x immediately before processing this query.
  • 2 x y: Remove all elements between x and y in A, and output the sum of the values of the removed elements. Specifically, let A_j be the j-th element of the current A and n be the length of A. For p and q such that A_p=x and A_q=y, output A_{\min(p,q)+1} + \dots + A_{\max(p,q)-1} and update A to (A_1,\dots,A_{\min(p,q)},A_{\max(p,q)},\dots,A_n). It is guaranteed that A contains both x and y immediately before processing this query.

Note that for any sequence of queries, the same value never appears multiple times in A during the process of handling queries, and thus the position where a value appears in A is unique (if it exists).

Constraints

  • 1\leq Q \leq 5\times 10^5
  • For the i-th query:
    • If it is a type 1 query:
      • 0\leq x < i
      • A contains x immediately before processing the query.
    • If it is a type 2 query:
      • 0\leq x < y < i
      • A contains both x and y immediately before processing the query.
  • All input values are integers.

Input

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

Q
\text{query}_{1}
\text{query}_{2}
\vdots
\text{query}_{Q}

Here, \text{query}_{i} represents the i-th query and is given in one of the following forms:

1 x
2 x y

Output

Let q be the number of type 2 queries. Output q lines. The i-th line should contain the value to be output for the i-th type 2 query.


Sample Input 1

6
1 0
1 1
1 0
2 2 3
1 2
2 0 5

Sample Output 1

1
5

Initially, A=(0).

  • 1st query: Insert 1 immediately after 0. A becomes (0,1).
  • 2nd query: Insert 2 immediately after 1. A becomes (0,1,2).
  • 3rd query: Insert 3 immediately after 0. A becomes (0,3,1,2).
  • 4th query: Remove the elements between 2 and 3, namely 1, and output the sum of the removed values, which is 1. A becomes (0,3,2).
  • 5th query: Insert 5 immediately after 2. A becomes (0,3,2,5).
  • 6th query: Remove the elements between 0 and 5, namely 3,2, and output the sum of the removed values, which is 5. A becomes (0,5).

Sample Input 2

2
1 0
2 0 1

Sample Output 2

0

In the 2nd query, we remove all elements between 0 and 1, but there are actually no such elements, so no elements are removed and the output value is 0.


Sample Input 3

10
1 0
1 1
2 0 2
2 0 2
1 0
1 5
2 0 5
2 2 6
1 6
1 9

Sample Output 3

1
0
0
0