A - 2UP3DOWN

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君が 100 階建てのビルにいます。

高橋君は 2 階分までの上り、または、3 階分までの下りであれば移動には階段を使い、そうでないときエレベーターを使います。

高橋君が X 階から Y 階への移動に使うのは階段ですか?

制約

  • 1 \leq X,Y \leq 100
  • X \neq Y
  • 入力は全ては整数である

入力

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

X Y

出力

移動に使うのが階段ならば Yes、エレベーターならば No を出力せよ。


入力例 1

1 4

出力例 1

No

1 階から 4 階への移動は 3 階分の上りなのでエレベーターを使います。


入力例 2

99 96

出力例 2

Yes

99 階から 96 階への移動は 3 階分の下りなので階段を使います。


入力例 3

100 1

出力例 3

No

Score : 100 points

Problem Statement

Takahashi is in a building with 100 floors.

He uses the stairs for moving up two floors or less or moving down three floors or less, and uses the elevator otherwise.

Does he use the stairs to move from floor X to floor Y?

Constraints

  • 1 \leq X,Y \leq 100
  • X \neq Y
  • All input values are integers.

Input

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

X Y

Output

If Takahashi uses the stairs for the move, print Yes; if he uses the elevator, print No.


Sample Input 1

1 4

Sample Output 1

No

The move from floor 1 to floor 4 involves going up three floors, so Takahashi uses the elevator.


Sample Input 2

99 96

Sample Output 2

Yes

The move from floor 99 to floor 96 involves going down three floors, so Takahashi uses the stairs.


Sample Input 3

100 1

Sample Output 3

No
B - ASCII code

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字 a, b, \ldots, z の ASCII 文字コードはこの順に 97,98,\ldots,122 です。

97 以上 122 以下の整数 N が与えられるので、ASCII 文字コードが N であるような英小文字を出力してください。

制約

  • N97 以上 122 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

97

出力例 1

a

ASCII 文字コードが 97 である英小文字は a です。


入力例 2

122

出力例 2

z

Score : 100 points

Problem Statement

The ASCII values of the lowercase English letters a, b, \ldots, z are 97,98,\ldots,122 in this order.

Given an integer N between 97 and 122, print the letter whose ASCII value is N.

Constraints

  • N is an integer between 97 and 122 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

97

Sample Output 1

a

97 is the ASCII value of a.


Sample Input 2

122

Sample Output 2

z
C - Langton's Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

HW 列のグリッドがあり、はじめすべてのマスが白で塗られています。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

このグリッドはトーラス状であるとみなします。すなわち、各 1 \leq i \leq H に対して (i, W) の右に (i, 1) があり、各 1 \leq j \leq W に対して (H, j) の下に (1, j) があるとします。

高橋君が (1, 1) にいて上を向いています。高橋君が以下の操作を N 回繰り返した後のグリッドの各マスがどの色で塗られているか出力してください。

  • 現在いるマスが白で塗られている場合は、現在いるマスを黒に塗り替え、時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。そうでない場合は、現在いるマスを白に塗り替え、反時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。

制約

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • 入力される数値はすべて整数

入力

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

H W N

出力

H 行出力せよ。i 行目には長さ W の文字列であって、(i, j) が白で塗られている場合は j 文字目が .、黒で塗られている場合は j 文字目が # であるものを出力せよ。


入力例 1

3 4 5

出力例 1

.#..
##..
....

グリッドの各マスは操作によって以下のように変化します。

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

入力例 2

2 2 1000

出力例 2

..
..

入力例 3

10 10 10

出力例 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#

Score: 250 points

Problem Statement

There is a grid with H rows and W columns; initially, all cells are painted white. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

This grid is considered to be toroidal. That is, (i, 1) is to the right of (i, W) for each 1 \leq i \leq H, and (1, j) is below (H, j) for each 1 \leq j \leq W.

Takahashi is at (1, 1) and facing upwards. Print the color of each cell in the grid after Takahashi repeats the following operation N times.

  • If the current cell is painted white, repaint it black, rotate 90^\circ clockwise, and move forward one cell in the direction he is facing. Otherwise, repaint the current cell white, rotate 90^\circ counterclockwise, and move forward one cell in the direction he is facing.

Constraints

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • All input values are integers.

Input

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

H W N

Output

Print H lines. The i-th line should contain a string of length W where the j-th character is . if the cell (i, j) is painted white, and # if it is painted black.


Sample Input 1

3 4 5

Sample Output 1

.#..
##..
....

The cells of the grid change as follows due to the operations:

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

Sample Input 2

2 2 1000

Sample Output 2

..
..

Sample Input 3

10 10 10

Sample Output 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#
D - Garbage Collection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 市では、N 種類のゴミを定期的に収集しています。i\;(=1,2,\dots,N) 種類目のゴミは、日付を q_i で割ったあまりが r_i の日に収集されます。

Q 個の質問に答えてください。j\;(=1,2,\dots,Q) 番目の質問では、d_j 日に t_j 種類目のゴミが出たときに、次にそれが収集される日を答えてください。

ただし、i 種類目のゴミが出た日が、 i 種類目のゴミが回収される日であった場合、そのゴミは同じ日に収集されるとします。

制約

  • 1 \leq N \leq 100
  • 0 \leq r_i < q_i \leq 10^9
  • 1 \leq Q \leq 100
  • 1 \leq t_j \leq N
  • 1 \leq d_j \leq 10^9
  • 入力はすべて整数

入力

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

N
q_1 r_1
q_2 r_2
\vdots
q_N r_N
Q
t_1 d_1
t_2 d_2
\vdots
t_Q d_Q

出力

Q 行出力せよ。j\;(1\leq j \leq Q) 行目には、j 番目の質問に対する答えを出力せよ。


入力例 1

2
7 3
4 2
5
1 1
1 3
1 4
1 15
2 7

出力例 1

3
3
10
17
10
  • 1 番目の質問:1 種類目のゴミが 1 日以降に初めて回収されるのは 3 日です。
  • 2 番目の質問:1 種類目のゴミが 3 日以降に初めて回収されるのは 3 日です。
  • 3 番目の質問:1 種類目のゴミが 4 日以降に初めて回収されるのは 10 日です。

Score : 200 points

Problem Statement

In AtCoder City, N types of garbage are collected regularly. The i-th type of garbage (i=1,2,\dots,N) is collected on days when the date modulo q_i equals r_i.

Answer Q queries. In the j-th query (j=1,2,\dots,Q), given that the t_j-th type of garbage is put out on day d_j, answer the next day on which it will be collected.

Here, if the i-th type of garbage is put out on a day when that type of garbage is collected, then the garbage will be collected on the same day.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq r_i < q_i \leq 10^9
  • 1 \leq Q \leq 100
  • 1 \leq t_j \leq N
  • 1 \leq d_j \leq 10^9
  • All input values are integers.

Input

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

N
q_1 r_1
q_2 r_2
\vdots
q_N r_N
Q
t_1 d_1
t_2 d_2
\vdots
t_Q d_Q

Output

Print Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j-th query.


Sample Input 1

2
7 3
4 2
5
1 1
1 3
1 4
1 15
2 7

Sample Output 1

3
3
10
17
10
  • 1st query: The 1st type of garbage is collected on day 3 for the first time after day 1.
  • 2nd query: The 1st type of garbage is collected on day 3 for the first time after day 3.
  • 3rd query: The 1st type of garbage is collected on day 10 for the first time after day 4.
E - 2^a b^2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

正の整数 X は、次の条件をみたすときかつその時に限り、良い整数と呼ばれます。

  • 正の整数の組 (a,b) を用いて、X=2^a\times b^2 と書ける。

例えば、400400=2^2\times 10^2 と書けるため、良い整数です。

正の整数 N が与えられるので、1 以上 N 以下の良い整数の個数を求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • N は整数

入力

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

N

出力

1 以上 N 以下の良い整数の個数を出力せよ。


入力例 1

20

出力例 1

5

1 以上 20 以下の良い整数は 2,4,8,16,185 つです。
よって、5 を出力します。


入力例 2

400

出力例 2

24

入力例 3

1234567890

出力例 3

42413

入力が 32bit 整数型に収まるとは限らないことに注意してください。

Score : 350 points

Problem Statement

A positive integer X is called a good integer if and only if it satisfies the following condition:

  • There exists a pair of positive integers (a,b) such that X = 2^a \times b^2.

For example, 400 is a good integer because 400 = 2^2 \times 10^2.

Given a positive integer N, find the number of good integers between 1 and N, inclusive.

Constraints

  • 1 \leq N \leq 10^{18}
  • N is an integer.

Input

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

N

Output

Print the number of good integers between 1 and N, inclusive.


Sample Input 1

20

Sample Output 1

5

There are five good integers between 1 and 20: 2, 4, 8, 16, and 18.
Thus, print 5.


Sample Input 2

400

Sample Output 2

24

Sample Input 3

1234567890

Sample Output 3

42413

Note that the input might not fit in a 32-bit integer type.

F - Variety Split Easy

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

この問題は F 問題の簡易版です。

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A1 か所で区切って 2 個の空でない連続する部分列に分割するとき、数列に含まれる数の種類数の和としてありえる最大値を求めてください。

より厳密には、1 \leq i \leq N-1 を満たす整数 i について (A_1,A_2,\ldots,A_i), (A_{i+1},A_{i+2},\ldots,A_N) のそれぞれに含まれる整数の種類数の和としてありえる最大値を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • 入力はすべて整数である

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

5
  • i=1 としたとき、(3)(1,4,1,5) のそれぞれに含まれる整数の種類数は 1,3 で、これらの和は 4 です。
  • i=2 としたとき、(3,1)(4,1,5) のそれぞれに含まれる整数の種類数は 2,3 で、これらの和は 5 です。
  • i=3 としたとき、(3,1,4)(1,5) のそれぞれに含まれる整数の種類数は 3,2 で、これらの和は 5 です。
  • i=4 としたとき、(3,1,4,1)(5) のそれぞれに含まれる整数の種類数は 3,1 で、これらの和は 4 です。

よって、 i=2,3 のときに、最大値 5 を取ります。


入力例 2

10
2 5 6 5 2 1 7 9 7 2

出力例 2

8

Score : 350 points

Problem Statement

This problem is a simplified version of Problem F.

You are given an integer sequence of length N: A = (A_1, A_2, \ldots, A_N).

When splitting A at one position into two non-empty (contiguous) subarrays, find the maximum possible sum of the counts of distinct integers in those subarrays.

More formally, find the maximum sum of the following two values for an integer i such that 1 \leq i \leq N-1: the count of distinct integers in (A_1, A_2, \ldots, A_i), and the count of distinct integers in (A_{i+1}, A_{i+2}, \ldots, A_N).

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

5
  • For i=1, (3) contains 1 distinct integer, and (1,4,1,5) contains 3 distinct integers, for a total of 4.
  • For i=2, (3,1) contains 2 distinct integers, and (4,1,5) contains 3 distinct integers, for a total of 5.
  • For i=3, (3,1,4) contains 3 distinct integers, and (1,5) contains 2 distinct integers, for a total of 5.
  • For i=4, (3,1,4,1) contains 3 distinct integers, and (5) contains 1 distinct integer, for a total of 4.

Therefore, the maximum sum is 5 for i=2,3.


Sample Input 2

10
2 5 6 5 2 1 7 9 7 2

Sample Output 2

8
G - Polyomino

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

いくつかの正方形を辺でつなげてできる、連結な多角形の形をしたパズルのピースのことを ポリオミノ と呼びます。

4 マス、横 4 マスのグリッドと、グリッドに収まる大きさの 3 個のポリオミノがあります。
i 番目のポリオミノの形は 16 個の文字 P_{i,j,k} (1 \leq j, k \leq 4) によって表されます。P_{i, j, k} は何も置かれていないグリッドに i 番目のポリオミノを置いたときの状態を意味して、P_{i, j, k}# の場合は上から j 行目、左から k 列目のマスにポリオミノが置かれていることを、. の場合は置かれていないことを意味します。(入出力例 1 の図も参考にしてください。)

あなたは次の条件を全て満たすように 3 個のポリオミノ全てをグリッドに敷き詰めることにしました。

  • グリッドの全てのマスはポリオミノで覆われている。
  • ポリオミノ同士が重なるように置くことはできない。
  • ポリオミノがグリッドからはみ出るように置くことはできない。
  • ポリオミノの平行移動と回転は自由に行うことができるが、裏返すことはできない。

条件を満たすようにグリッドにポリオミノを敷き詰めることは可能ですか?

制約

  • P_{i, j, k}# または .
  • 与えられるポリオミノは連結である。つまり、ポリオミノを構成する正方形同士は、正方形のみを上下左右に辿って互いに行き来できる
  • 与えられるポリオミノは空でない

入力

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

P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}

出力

問題文の条件を満たすようにポリオミノを敷き詰めることが可能である場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

....
###.
.#..
....
....
.###
.##.
....
..#.
.##.
.##.
.##.

出力例 1

Yes

入力例 1 に対応するポリオミノの形は次の図のようになります。

image1

この場合、次の図のようにポリオミノを配置することで、問題文の条件を満たすようにグリッドにポリオミノを敷き詰めることができます。

image2

よって答えは Yes になります。


入力例 2

###.
#.#.
##..
....
....
..#.
....
....
####
##..
#...
#...

出力例 2

Yes

入力例 21 番目のポリオミノのように、ポリオミノは穴の空いた多角形の形をしている場合があります。


入力例 3

##..
#..#
####
....
....
##..
.##.
....
.#..
.#..
.#..
.#..

出力例 3

No

ポリオミノを敷き詰めるときに、ポリオミノを裏返してはならないのに注意してください。


入力例 4

....
..#.
....
....
....
..#.
....
....
....
..#.
....
....

出力例 4

No

入力例 5

....
####
#...
#...
....
####
...#
..##
....
..##
..#.
..##

出力例 5

No

入力例 6

###.
.##.
..#.
.###
....
...#
..##
...#
....
#...
#...
#...

出力例 6

Yes

Score : 400 points

Problem Statement

A polyomino is a puzzle piece in the shape of a connected polygon made by connecting several squares by their edges.

There is a grid with four rows and four columns, and three polyominoes that fit within the grid.
The shape of the i-th polyomino is represented by 16 characters P_{i,j,k} (1 \leq j, k \leq 4). They describe the state of the grid when the i-th polyomino is placed on it. If P_{i, j, k} is #, the square at the j-th row from the top and k-th column from the left is occupied by the polyomino; if it is ., the square is not occupied. (Refer to the figures at Sample Input/Output 1.)

You want to fill the grid with all three polyominoes so that all of the following conditions are satisfied.

  • All squares of the grid are covered by the polyominoes.
  • The polyominoes must not overlap each other.
  • The polyominoes must not stick out of the grid.
  • The polyominoes may be freely translated and rotated but may not be flipped over.

Can the grid be filled with the polyominoes to satisfy these conditions?

Constraints

  • P_{i, j, k} is # or ..
  • The given polyominoes are connected. In other words, the squares that make up a polyomino can be reached from each other by following only the squares up, down, left, and right.
  • The given polyominoes are not empty.

Input

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

P_{1,1,1}P_{1,1,2}P_{1,1,3}P_{1,1,4}
P_{1,2,1}P_{1,2,2}P_{1,2,3}P_{1,2,4}
P_{1,3,1}P_{1,3,2}P_{1,3,3}P_{1,3,4}
P_{1,4,1}P_{1,4,2}P_{1,4,3}P_{1,4,4}
P_{2,1,1}P_{2,1,2}P_{2,1,3}P_{2,1,4}
P_{2,2,1}P_{2,2,2}P_{2,2,3}P_{2,2,4}
P_{2,3,1}P_{2,3,2}P_{2,3,3}P_{2,3,4}
P_{2,4,1}P_{2,4,2}P_{2,4,3}P_{2,4,4}
P_{3,1,1}P_{3,1,2}P_{3,1,3}P_{3,1,4}
P_{3,2,1}P_{3,2,2}P_{3,2,3}P_{3,2,4}
P_{3,3,1}P_{3,3,2}P_{3,3,3}P_{3,3,4}
P_{3,4,1}P_{3,4,2}P_{3,4,3}P_{3,4,4}

Output

If it is possible to fill the grid with the polyominoes to satisfy the conditions in the problem statement, print Yes; otherwise, print No.


Sample Input 1

....
###.
.#..
....
....
.###
.##.
....
..#.
.##.
.##.
.##.

Sample Output 1

Yes

The figure below shows the shapes of the polyominoes corresponding to Sample Input 1.

image1

In this case, you can fill the grid with them to satisfy the conditions in the problem statement by placing them as shown in the figure below.

image2

Thus, the answer is Yes.


Sample Input 2

###.
#.#.
##..
....
....
..#.
....
....
####
##..
#...
#...

Sample Output 2

Yes

As in the first polyomino in Sample Input 2, a polyomino may be in the shape of a polygon with a hole.


Sample Input 3

##..
#..#
####
....
....
##..
.##.
....
.#..
.#..
.#..
.#..

Sample Output 3

No

Note that the polyominoes may not be flipped over when filling the grid.


Sample Input 4

....
..#.
....
....
....
..#.
....
....
....
..#.
....
....

Sample Output 4

No

Sample Input 5

....
####
#...
#...
....
####
...#
..##
....
..##
..#.
..##

Sample Output 5

No

Sample Input 6

###.
.##.
..#.
.###
....
...#
..##
...#
....
#...
#...
#...

Sample Output 6

Yes
H - Chain Contestant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

別世界の AtCoder では現在、 AAC, ..., AJC の 10 種類のコンテストが開催されており、これから N 回のコンテストが開催されます。
各コンテストの種類は文字列 S として与えられ、 Si 文字目が x なら i 回目には AxC が開催されます。
シカの AtCoDeer くんは、 N 個のコンテストから 1 個以上いくつか選んで、以下の条件を満たすように選んで出場します。

  • 出るコンテストを順番を保ったまま抜き出したとき、コンテストの種類ごとにひとかたまりとなっている。
    • 厳密には、 AtCoDeer くんが x 個のコンテストに出場し、そのうち i 回目のコンテストの種類が T_i であるとき、全ての 1 \le i < j < k \le x を満たす整数組 (i,j,k) に対して、 T_i=T_k であるならば T_i=T_j でなければならない。

AtCoDeer くんが出場するコンテストの選び方として考えられるものの総数を 998244353 で割った余りを求めてください。
ただし、 2 つのコンテストの選び方が異なるとは、あるコンテスト c が存在して、片方の選び方では c に出場するがもう片方の選び方では出場しないということを指します。

制約

  • 1 \le N \le 1000
  • |S|=N
  • SA から J までの英大文字のみからなる

入力

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

N
S

出力

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


入力例 1

4
BGBH

出力例 1

13

例えば、 1,3 回目のコンテストに出場する、 2,4 回目のコンテストに出場するという選び方は条件を満たします。
一方、 1,2,3,4 回目のコンテストに出場する場合、 ABC への出場がひとかたまりになっておらず、整数組 (i,j,k)=(1,2,3) について条件に違反します。
また、全てのコンテストに出場しないということも認められません。
問題文の条件に適する出場するコンテストの選び方は 13 通りあります。


入力例 2

100
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF

出力例 2

330219020

総数を 998244353 で割った余りを求めることに注意してください。

Score : 500 points

Problem Statement

AtCoder in another world holds 10 types of contests called AAC, ..., AJC. There will be N contests from now on.
The types of these N contests are given to you as a string S: if the i-th character of S is x, the i-th contest will be AxC.
AtCoDeer will choose and participate in one or more contests from the N so that the following condition is satisfied.

  • In the sequence of contests he will participate in, the contests of the same type are consecutive.
    • Formally, when AtCoDeer participates in x contests and the i-th of them is of type T_i, for every triple of integers (i,j,k) such that 1 \le i < j < k \le x, T_i=T_j must hold if T_i=T_k.

Find the number of ways for AtCoDeer to choose contests to participate in, modulo 998244353.
Two ways to choose contests are considered different when there is a contest c such that AtCoDeer participates in c in one way but not in the other.

Constraints

  • 1 \le N \le 1000
  • |S|=N
  • S consists of uppercase English letters from A through J.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the answer as an integer.


Sample Input 1

4
BGBH

Sample Output 1

13

For example, participating in the 1-st and 3-rd contests is valid, and so is participating in the 2-nd and 4-th contests.
On the other hand, participating in the 1-st, 2-nd, 3-rd, and 4-th contests is invalid, since the participations in ABCs are not consecutive, violating the condition for the triple (i,j,k)=(1,2,3).
Additionally, it is not allowed to participate in zero contests.
In total, there are 13 valid ways to participate in some contests.


Sample Input 2

100
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF

Sample Output 2

330219020

Be sure to find the count modulo 998244353.

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