Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
AtCoder 王国の住民は A 時になるとたこ焼きへの愛を叫ぶことになっています。
AtCoder 王国に住む高橋君は毎日 B 時に就寝し C 時に起床します。高橋君は、起きているときはたこ焼きへの愛を叫ぶことができ、寝ているときは叫ぶことができません。高橋君が毎日たこ焼きへの愛を叫ぶことができているか判定してください。ただし、一日は 24 時間であり、高橋君が寝ている時間は 24 時間未満であるとします。
制約
- 0\leq A,B,C\lt 24
- A,B,C は相異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B C
出力
高橋君が毎日たこ焼きへの愛を叫ぶことができているならば Yes を、そうでないならば No を出力せよ。
入力例 1
21 8 14
出力例 1
Yes
高橋君は毎日 8 時に就寝し 14 時に起床します。21 時には起きているため、高橋君は毎日たこ焼きへの愛を叫ぶことができます。よって Yes を出力します。
入力例 2
0 21 7
出力例 2
No
高橋君は毎日 21 時に就寝し 7 時に起床します。0 時には起きていないため、高橋君は毎日たこ焼きへの愛を叫ぶことができません。よって No を出力します。
入力例 3
10 7 17
出力例 3
No
Score : 150 points
Problem Statement
In the Kingdom of AtCoder, residents are required to shout their love for takoyaki at A o'clock every day.
Takahashi, who lives in the Kingdom of AtCoder, goes to bed at B o'clock and wakes up at C o'clock every day (in the 24-hour clock). He can shout his love for takoyaki when he is awake, but cannot when he is asleep. Determine whether he can shout his love for takoyaki every day. Here, a day has 24 hours, and his sleeping time is less than 24 hours.
Constraints
- 0\leq A,B,C\lt 24
- A, B, and C are pairwise different.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B C
Output
Print Yes if Takahashi can shout his love for takoyaki every day, and No otherwise.
Sample Input 1
21 8 14
Sample Output 1
Yes
Takahashi goes to bed at 8 o'clock and wakes up at 14 o'clock every day. He is awake at 21 o'clock, so he can shout his love for takoyaki every day. Therefore, print Yes.
Sample Input 2
0 21 7
Sample Output 2
No
Takahashi goes to bed at 21 o'clock and wakes up at 7 o'clock every day. He is not awake at 0 o'clock, so he cannot shout his love for takoyaki every day. Therefore, print No.
Sample Input 3
10 7 17
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
数直線があり、高橋君はこれを赤色と青色で次のように塗りました。
- X=L_1 から X=R_1 までをすべて赤色で塗る。
- X=L_2 から X=R_2 までをすべて青色で塗る。
数直線のうち、赤色と青色の両方で塗られている部分の長さを求めてください。
制約
- 0\leq L_1<R_1\leq 100
- 0\leq L_2<R_2\leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
L_1 R_1 L_2 R_2
出力
両方の色で塗られている部分の長さを整数で出力せよ。
入力例 1
0 3 1 5
出力例 1
2
X=0 から X=3 までが赤く、 X=1 から X=5 までが青く塗られています。
よって、両方の色で塗られている部分は X=1 から X=3 までであり、その長さは 2 となります。
入力例 2
0 1 4 5
出力例 2
0
両方の色で塗られている部分はありません。
入力例 3
0 3 3 7
出力例 3
0
赤色と青色で塗られている部分が接している場合でも、 両方の色で塗られている部分の長さは 0 となります。
Score : 100 points
Problem Statement
We have a number line. Takahashi painted some parts of this line, as follows:
- First, he painted the part from X=L_1 to X=R_1 red.
- Next, he painted the part from X=L_2 to X=R_2 blue.
Find the length of the part of the line painted both red and blue.
Constraints
- 0\leq L_1<R_1\leq 100
- 0\leq L_2<R_2\leq 100
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
L_1 R_1 L_2 R_2
Output
Print the length of the part of the line painted both red and blue, as an integer.
Sample Input 1
0 3 1 5
Sample Output 1
2
The part from X=0 to X=3 is painted red, and the part from X=1 to X=5 is painted blue.
Thus, the part from X=1 to X=3 is painted both red and blue, and its length is 2.
Sample Input 2
0 1 4 5
Sample Output 2
0
No part is painted both red and blue.
Sample Input 3
0 3 3 7
Sample Output 3
0
If the part painted red and the part painted blue are adjacent to each other, the length of the part painted both red and blue is 0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
N 個の整数 A_1,A_2,\dots,A_N が、 1 行に 1 つずつ、 N 行にわたって与えられます。但し、 N は入力では与えられません。
さらに、以下が保証されます。
- A_i \neq 0 ( 1 \le i \le N-1 )
- A_N = 0
A_N, A_{N-1},\dots,A_1 をこの順に出力してください。
制約
- 入力は全て整数
- 1 \le N \le 100
- 1 \le A_i \le 10^9 ( 1 \le i \le N-1 )
- A_N = 0
入力
入力は以下の形式で標準入力から与えられる。
A_1 A_2 \vdots A_N
出力
A_N, A_{N-1},\dots,A_1 をこの順に、改行区切りで整数として出力せよ。
入力例 1
3 2 1 0
出力例 1
0 1 2 3
繰り返しになりますが、 N は入力では与えられないことに注意してください。
この入力においては N=4 で、 A=(3,2,1,0) です。
入力例 2
0
出力例 2
0
A=(0) です。
入力例 3
123 456 789 987 654 321 0
出力例 3
0 321 654 987 789 456 123
Score: 150 points
Problem Statement
You are given N integers A_1,A_2,\dots,A_N, one per line, over N lines. However, N is not given in the input.
Furthermore, the following is guaranteed:
- A_i \neq 0 ( 1 \le i \le N-1 )
- A_N = 0
Print A_N, A_{N-1},\dots,A_1 in this order.
Constraints
- All input values are integers.
- 1 \le N \le 100
- 1 \le A_i \le 10^9 ( 1 \le i \le N-1 )
- A_N = 0
Input
The input is given from Standard Input in the following format:
A_1 A_2 \vdots A_N
Output
Print A_N, A_{N-1}, \dots, A_1 in this order, as integers, separated by newlines.
Sample Input 1
3 2 1 0
Sample Output 1
0 1 2 3
Note again that N is not given in the input. Here, N=4 and A=(3,2,1,0).
Sample Input 2
0
Sample Output 2
0
A=(0).
Sample Input 3
123 456 789 987 654 321 0
Sample Output 3
0 321 654 987 789 456 123
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 A 行、横 B 列のマスからなるタイルを縦 N 行、横 N 列に並べてできた、縦 (A\times N) 行、横 (B\times N) 列のマス目 X があります。
1\leq i,j \leq N について、上から i 行目、左から j 列目のタイルをタイル (i,j) とします。
X の各マスは以下のように塗られています。
- 各タイルは白いタイルまたは黒いタイルである。
- 白いタイルのすべてのマスは白で塗られ、黒いタイルのすべてのマスは黒で塗られている。
- タイル (1,1) は白いタイルである。
- 辺で隣接する 2 つのタイルは異なる色のタイルである。ただし、タイル (a,b) とタイル (c,d) が辺で隣接するとは、|a-c|+|b-d|=1 ( |x| を x の絶対値とする)であることを言う。
マス目 X を出力の形式に従って出力してください。
制約
- 1 \leq N,A,B \leq 10
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A B
出力
次の条件をみたす (A\times N) 個の文字列 S_1,\ldots,S_{A\times N} を改行区切りで出力せよ。
- S_1,\ldots,S_{A\times N} はそれぞれ長さ (B\times N) の
.または#からなる文字列である。 - 各 i,j (1 \leq i \leq A\times N,1 \leq j \leq B\times N) に対し、マス目 X の上から i 行目かつ左から j 列目のマスが白で塗られているならば S_i の j 文字目は
.であり、黒く塗られているならば#である。
入力例 1
4 3 2
出力例 1
..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##.. ..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##..
入力例 2
5 1 5
出力例 2
.....#####.....#####..... #####.....#####.....##### .....#####.....#####..... #####.....#####.....##### .....#####.....#####.....
入力例 3
4 4 1
出力例 3
.#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#. .#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#.
入力例 4
1 4 4
出力例 4
.... .... .... ....
Score : 200 points
Problem Statement
Tiles are aligned in N horizontal rows and N vertical columns. Each tile has a grid with A horizontal rows and B vertical columns. On the whole, the tiles form a grid X with (A\times N) horizontal rows and (B\times N) vertical columns.
For 1\leq i,j \leq N, Tile (i,j) denotes the tile at the i-th row from the top and the j-th column from the left.
Each square of X is painted as follows.
- Each tile is either a white tile or a black tile.
- Every square in a white tile is painted white; every square in a black tile is painted black.
- Tile (1,1) is a white tile.
- Two tiles sharing a side have different colors. Here, Tile (a,b) and Tile (c,d) are said to be sharing a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).
Print the grid X in the format specified in the Output section.
Constraints
- 1 \leq N,A,B \leq 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print (A\times N) strings S_1,\ldots,S_{A\times N} that satisfy the following condition, with newlines in between.
- Each of S_1,\ldots,S_{A\times N} is a string of length (B\times N) consisting of
.and#. - For each i and j (1 \leq i \leq A\times N,1 \leq j \leq B\times N), the j-th character of S_i is
.if the square at the i-th row from the top and j-th column from the left in grid X is painted white; the character is#if the square is painted black.
Sample Input 1
4 3 2
Sample Output 1
..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##.. ..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##..
Sample Input 2
5 1 5
Sample Output 2
.....#####.....#####..... #####.....#####.....##### .....#####.....#####..... #####.....#####.....##### .....#####.....#####.....
Sample Input 3
4 4 1
Sample Output 3
.#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#. .#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#.
Sample Input 4
1 4 4
Sample Output 4
.... .... .... ....
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
1\leq l\leq r\leq N を満たす整数の組 (l,r) であって、数列 (A_l,A_{l+1},\dots,A_r) が等差数列であるようなものが何通りあるか求めてください。
なお、数列 (x_1,x_2,\dots,x_{|x|}) が等差数列であるとは、ある d が存在して x_{i+1}-x_i=d\ (1\leq i < |x|) であることをいいます。 特に、長さ 1 の数列は常に等差数列です。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 3 6 9 3
出力例 1
8
条件を満たす整数の組 (l,r) は (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3) の 8 通りです。
実際、(l,r)=(1,3) のとき (A_l,\dots,A_r)=(3,6,9) は等差数列なので条件を満たしますが、 (l,r)=(2,4) のとき (A_l,\dots,A_r)=(6,9,3) は等差数列ではないので条件を満たしません。
入力例 2
5 1 1 1 1 1
出力例 2
15
すべての整数の組 (l,r)\ (1\leq l\leq r\leq 5) が条件を満たします。
入力例 3
8 87 42 64 86 72 58 44 30
出力例 3
22
Score : 300 points
Problem Statement
You are given a sequence of N positive integers A=(A_1,A_2,\dots,A_N).
Find the number of pairs of integers (l,r) satisfying 1\leq l\leq r\leq N such that the subsequence (A_l,A_{l+1},\dots,A_r) forms an arithmetic progression.
A sequence (x_1,x_2,\dots,x_{|x|}) is an arithmetic progression if and only if there exists a d such that x_{i+1}-x_i=d\ (1\leq i < |x|). In particular, a sequence of length 1 is always an arithmetic progression.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 3 6 9 3
Sample Output 1
8
There are eight pairs of integers (l,r) satisfying the condition: (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3).
Indeed, when (l,r)=(1,3), (A_l,\dots,A_r)=(3,6,9) is an arithmetic progression, so it satisfies the condition. However, when (l,r)=(2,4), (A_l,\dots,A_r)=(6,9,3) is not an arithmetic progression, so it does not satisfy the condition.
Sample Input 2
5 1 1 1 1 1
Sample Output 2
15
All pairs of integers (l,r)\ (1\leq l\leq r\leq 5) satisfy the condition.
Sample Input 3
8 87 42 64 86 72 58 44 30
Sample Output 3
22
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は座標平面上で龍を操作するゲームを作成しました。
龍は 1 から N までの番号がついた N 個のパーツからなり、パーツ 1 を頭 と呼びます。
最初パーツ i は座標 (i,0) にあります。以下のクエリを Q 個処理してください。
1 C: 頭を方向 C に 1 移動させる。ここで、C はR,L,U,Dのいずれかであり、それぞれ x 軸正方向、x 軸負方向、y 軸正方向、y 軸負方向を意味する。頭以外の全てのパーツは前のパーツに追従するように動く。すなわち、パーツ i\ \ (2\leq i \leq N) は移動前にパーツ i-1 があった座標に移動する。2 p: パーツ p のある座標を求める。
制約
- 2 \leq N \leq 10^6
- 1 \leq Q \leq 2\times 10^5
- 1 種類目のクエリにおいて、C は
R,L,U,Dのいずれか - 2 種類目のクエリにおいて、1\leq p \leq N
- 入力に含まれる数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
各クエリは以下の 2 種類のいずれかの形式である。
1 C
2 p
出力
2 種類目のクエリの回数を q として q 行出力せよ。
i 行目には、i 番目のそのようなクエリに対する答えの座標を (x,y) としたとき、x,y を空白区切りで出力せよ。
入力例 1
5 9 2 3 1 U 2 3 1 R 1 D 2 3 1 L 2 1 2 5
出力例 1
3 0 2 0 1 1 1 0 1 0
2 種類目のクエリを処理する各タイミングにおいて、パーツの位置は次のようになっています。

複数のパーツが同じ座標に存在しうることに注意してください。
Score : 300 points
Problem Statement
Takahashi has created a game where the player controls a dragon on a coordinate plane.
The dragon consists of N parts numbered 1 to N, with part 1 being called the head.
Initially, part i is located at the coordinates (i,0). Process Q queries as follows.
1 C: Move the head by 1 in direction C. Here, C is one ofR,L,U, andD, which represent the positive x-direction, negative x-direction, positive y-direction, and negative y-direction, respectively. Each part other than the head moves to follow the part in front of it. That is, part i (2\leq i \leq N) moves to the coordinates where part i-1 was before the move.2 p: Find the coordinates of part p.
Constraints
- 2 \leq N \leq 10^6
- 1 \leq Q \leq 2\times 10^5
- For the first type of query, C is one of
R,L,U, andD. - For the second type of query, 1\leq p \leq N.
- All numerical input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
Each query is in one of the following two formats:
1 C
2 p
Output
Print q lines, where q is the number of queries of the second type.
The i-th line should contain x and y separated by a space, where (x,y) are the answer to the i-th such query.
Sample Input 1
5 9 2 3 1 U 2 3 1 R 1 D 2 3 1 L 2 1 2 5
Sample Output 1
3 0 2 0 1 1 1 0 1 0
At each time when processing the second type of query, the parts are at the following positions:

Note that multiple parts may exist at the same coordinates.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N 行 N 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。
また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。
-
S_i の j 文字目が
Pであるとき、(i, j) は空きマスであり、プレイヤーがいる -
S_i の j 文字目が
.であるとき、(i, j) は空きマスであり、プレイヤーがいない -
S_i の j 文字目が
#であるとき、(i, j) は障害物のあるマスである
以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。
- 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。
制約
- N は 2 以上 60 以下の整数
- S_i は長さ N の
P,.,#からなる文字列 - S_i の j 文字目が
Pであるような組 (i, j) の個数はちょうど 2 つ
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
5 ....# #..#. .P... ..P.. ....#
出力例 1
3
はじめに (3, 2) にいるプレイヤーをプレイヤー 1、(4, 3) にいるプレイヤーをプレイヤー 2 とします。
例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。
-
左を選択する。プレイヤー 1 は (3, 1) に移動し、プレイヤー 2 は (4, 2) に移動する。
-
上を選択する。プレイヤー 1 は移動せず、プレイヤー 2 は (3, 2) に移動する。
-
左を選択する。プレイヤー 1 は移動せず、プレイヤー 2 は (3, 1) に移動する。
入力例 2
2 P# #P
出力例 2
-1
入力例 3
10 .......... .......... .......... .......... ....P..... .....P.... .......... .......... .......... ..........
出力例 3
10
Score: 400 points
Problem Statement
There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:
-
If the j-th character of S_i is
P, then (i, j) is an empty cell with a player on it. -
If the j-th character of S_i is
., then (i, j) is an empty cell without a player. -
If the j-th character of S_i is
#, then (i, j) contains an obstacle.
Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.
- Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.
Constraints
- N is an integer between 2 and 60, inclusive.
- S_i is a string of length N consisting of
P,., and#. - There are exactly two pairs (i, j) where the j-th character of S_i is
P.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
5 ....# #..#. .P... ..P.. ....#
Sample Output 1
3
Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.
For example, doing the following brings the two players to the same cell in three moves:
-
Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).
-
Choose up. Player 1 does not move, and Player 2 moves to (3, 2).
-
Choose left. Player 1 does not move, and Player 2 moves to (3, 1).
Sample Input 2
2 P# #P
Sample Output 2
-1
Sample Input 3
10 .......... .......... .......... .......... ....P..... .....P.... .......... .......... .......... ..........
Sample Output 3
10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
全ての要素が 0 で初期化された長さ N の整数列 A=(A_1,A_2,\ldots,A_N) があります。また、集合 S があります。はじめ S は空です。
以下の Q 個のクエリを順に行います。Q 個のクエリを全て処理した後の数列 A の各要素の値を求めてください。 i 番目のクエリは以下の形式です。
- 整数 x_i が与えられる。整数 x_i が S に含まれる場合、S から x_i を削除する。そうでない場合、S に x_i を追加する。次に、j=1,2,\ldots,N について、j\in S ならば A_j に |S| を加算する。
なお、|S| は集合 S の要素数を意味します。例えば S=\lbrace 3,4,7\rbrace のとき、|S|=3 です。
制約
- 1\leq N,Q\leq 2\times10^5
- 1\leq x_i\leq N
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q x_1 x_2 \ldots x_Q
出力
クエリを全て処理した後の数列 A を以下の形式で出力せよ。
A_1 A_2 \ldots A_N
入力例 1
3 4 1 3 3 2
出力例 1
6 2 2
1 番目のクエリでは、S に 1 を追加し、S=\lbrace 1\rbrace となります。その後、A_1 に |S|=1 を加算します。A=(1,0,0) となります。
2 番目のクエリでは、S に 3 を追加し、S=\lbrace 1,3\rbrace となります。その後、A_1,A_3 に |S|=2 を加算します。A=(3,0,2) となります。
3 番目のクエリでは、S から 3 を削除し、S=\lbrace 1\rbrace となります。その後、A_1 に |S|=1 を加算します。A=(4,0,2) となります。
4 番目のクエリでは、S に 2 を追加し、S=\lbrace 1,2\rbrace となります。その後、A_1,A_2 に |S|=2 を加算します。A=(6,2,2) となります。
最終的に、A=(6,2,2) となります。
入力例 2
4 6 1 2 3 2 4 2
出力例 2
15 9 12 7
Score: 500 points
Problem Statement
There is an integer sequence A=(A_1,A_2,\ldots,A_N) of length N, where all elements are initially set to 0. Also, there is a set S, which is initially empty.
Perform the following Q queries in order. Find the value of each element in the sequence A after processing all Q queries. The i-th query is in the following format:
- An integer x_i is given. If the integer x_i is contained in S, remove x_i from S. Otherwise, insert x_i to S. Then, for each j=1,2,\ldots,N, add |S| to A_j if j\in S.
Here, |S| denotes the number of elements in the set S. For example, if S=\lbrace 3,4,7\rbrace, then |S|=3.
Constraints
- 1\leq N,Q\leq 2\times10^5
- 1\leq x_i\leq N
- All given numbers are integers.
Input
The input is given from Standard Input in the following format:
N Q x_1 x_2 \ldots x_Q
Output
Print the sequence A after processing all queries in the following format:
A_1 A_2 \ldots A_N
Sample Input 1
3 4 1 3 3 2
Sample Output 1
6 2 2
In the first query, 1 is inserted to S, making S=\lbrace 1\rbrace. Then, |S|=1 is added to A_1. The sequence becomes A=(1,0,0).
In the second query, 3 is inserted to S, making S=\lbrace 1,3\rbrace. Then, |S|=2 is added to A_1 and A_3. The sequence becomes A=(3,0,2).
In the third query, 3 is removed from S, making S=\lbrace 1\rbrace. Then, |S|=1 is added to A_1. The sequence becomes A=(4,0,2).
In the fourth query, 2 is inserted to S, making S=\lbrace 1,2\rbrace. Then, |S|=2 is added to A_1 and A_2. The sequence becomes A=(6,2,2).
Eventually, the sequence becomes A=(6,2,2).
Sample Input 2
4 6 1 2 3 2 4 2
Sample Output 2
15 9 12 7
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2 つの文字列 X,Y に対して、その類似度 f(X,Y) を、X と Y を先頭から見て一致している文字数とします。
例えば abc と axbc の類似度は 1 、aaa と aaaa の類似度は 3 です。
長さ N の文字列 S が与えられます。S の i 文字目以降からなる文字列を S_i とします。k=1,\ldots,N のそれぞれについて、f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N) を求めてください。
制約
- 1 \leq N \leq 10^6
- S は英小文字のみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N 行出力せよ。
i 行目には k=i の問題の答えを出力せよ。
入力例 1
3 abb
出力例 1
3 3 2
S_1,S_2,S_3 はそれぞれ abb, bb, b です。
- k=1 のとき f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3
- k=2 のとき f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3
- k=3 のとき f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2
入力例 2
11 mississippi
出力例 2
11 16 14 12 13 11 9 7 4 3 4
Score : 500 points
Problem Statement
Let the similarity f(X,Y) between two strings X and Y be the length of their longest common prefix.
For example, the similarity between abc and axbc is 1, and the similarity between aaa and aaaa is 3.
You are given a string S of length N. Let S_i be the suffix of S beginning with the i-th character of S. For each k=1,\ldots,N, find f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N).
Constraints
- 1 \leq N \leq 10^6
- S is a string of length N consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N S
Output
Print N lines.
The i-th line should contain the answer for k=i.
Sample Input 1
3 abb
Sample Output 1
3 3 2
S_1 is abb, S_2 is bb, and S_3 is b.
- For k=1: f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3.
- For k=2: f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3.
- For k=3: f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2.
Sample Input 2
11 mississippi
Sample Output 2
11 16 14 12 13 11 9 7 4 3 4