Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は漢文の勉強をしていて、漢字を読む順番が分からず困っています。高橋君を助けましょう!
1 から N までの N 個の整数が小さい方から順に 1 列に並んでいます。
整数の間に M 個の「レ」が挟まっています。i 個目の「レ」は、整数 a_i と整数 a_i + 1 の間にあります。
あなたは次の手順に従って、N 個の整数を 1 回ずつ全て読みます。
- まず、頂点に 1 から N までの番号がついた N 頂点 M 辺の無向グラフ G を考える。i 本目の辺は頂点 a_i と頂点 a_i + 1 を結んでいる。
- そして、読まれていない整数が無くなるまで次の操作を繰り返す。
- 読まれていない整数のうち最小のものを x とする。頂点 x が含まれる連結成分 C を選び、C に含まれる頂点の番号を大きい方から順に全て読む。
例えば、整数と「レ」が

という順番で並んでいる場合を考えます。(この場合 N = 5, M = 3, a = (1, 3, 4) です。)
このとき、整数が読まれる順番は以下の手順によって 2, 1, 5, 4, 3 に決定します。
- 最初、読まれていない整数のうち最小のものは 1 であり、グラフ G の頂点 1 を含む連結成分に含まれる頂点は \lbrace 1, 2 \rbrace である。よって 2, 1 がこの順で読まれる。
- 次に、読まれていない整数のうち最小のものは 3 であり、グラフ G の頂点 3 を含む連結成分に含まれる頂点は \lbrace 3, 4, 5 \rbrace である。よって 5, 4, 3 がこの順で読まれる。
- すべての整数が読まれたので手順を終了する。
N, M, (a_1, a_2, \dots, a_M) が入力として与えられるので、 N 個の整数を読む順番を出力してください。
連結成分とは
あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。
制約
- 1 \leq N \leq 100
- 0 \leq M \leq N - 1
- 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M a_1 a_2 \dots a_M
出力
答えを以下の形式で出力せよ。ここで p_i は、i 番目に読まれる整数を意味する。
p_1 p_2 \dots p_N
入力例 1
5 3 1 3 4
出力例 1
2 1 5 4 3
問題文の例にある通り、整数と「レ」が

という順で並んでいる場合は 2, 1, 5, 4, 3 の順で読みます。
入力例 2
5 0
出力例 2
1 2 3 4 5
「レ」が存在しない場合もあります。
入力例 3
10 6 1 2 3 7 8 9
出力例 3
4 3 2 1 5 6 10 9 8 7
Score : 200 points
Problem Statement
Studying Kanbun, Takahashi is having trouble figuring out the order to read words. Help him out!
There are N integers from 1 through N arranged in a line in ascending order.
Between them are M "レ" marks. The i-th "レ" mark is between the integer a_i and integer (a_i + 1).
You read each of the N integers once by the following procedure.
- Consider an undirected graph G with N vertices numbered 1 through N and M edges. The i-th edge connects vertex a_i and vertex (a_i+1).
- Repeat the following operation until there is no unread integer:
- let x be the minimum unread integer. Choose the connected component C containing vertex x, and read all the numbers of the vertices contained in C in descending order.
For example, suppose that integers and "レ" marks are arranged in the following order:

(In this case, N = 5, M = 3, and a = (1, 3, 4).)
Then, the order to read the integers is determined to be 2, 1, 5, 4, and 3, as follows:
- At first, the minimum unread integer is 1, and the connected component of G containing vertex 1 has vertices \lbrace 1, 2 \rbrace, so 2 and 1 are read in this order.
- Then, the minimum unread integer is 3, and the connected component of G containing vertex 3 has vertices \lbrace 3, 4, 5 \rbrace, so 5, 4, and 3 are read in this order.
- Now that all integers are read, terminate the procedure.
Given N, M, and (a_1, a_2, \dots, a_M), print the order to read the N integers.
What is a connected component?
A subgraph of a graph is a graph obtained by choosing some vertices and edges from the original graph.A graph is said to be connected if and only if one can travel between any two vertices in the graph via edges.
A connected component is a connected subgraph that is not contained in any larger connected subgraph.
Constraints
- 1 \leq N \leq 100
- 0 \leq M \leq N - 1
- 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
- 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_M
Output
Print the answer in the following format, where p_i is the i-th integers to read.
p_1 p_2 \dots p_N
Sample Input 1
5 3 1 3 4
Sample Output 1
2 1 5 4 3
As described in the Problem Statement, if integers and "レ" marks are arranged in the following order:

then the integers are read in the following order: 2, 1, 5, 4, and 3.
Sample Input 2
5 0
Sample Output 2
1 2 3 4 5
There may be no "レ" mark.
Sample Input 3
10 6 1 2 3 7 8 9
Sample Output 3
4 3 2 1 5 6 10 9 8 7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
AtCoder Land の入り口には 1 つのチケット売り場があり、来園客はこのチケット売り場の前に一列に並んで順にチケットを購入します。 チケットの購入手続きには一人当たり A 秒かかり、列の先頭の人がチケットを購入し終わると、(存在すれば)次の人がすぐさま購入手続きを開始します。
現在チケット売り場に並んでいる人はおらず、今から N 人の人が順にチケットを買いに来ます。 具体的には、i 番目の人は今から T_i 秒後にチケット売り場を訪れ、既に列が存在すればその最後尾に並び、存在しなければすぐさま購入手続きを開始します。 ここで、T_1< T_2< \dots < T_N です。
各 i\ (1\leq i\leq N) について、i 番目の人がチケットを購入し終わるのは今から何秒後か求めてください。
制約
- 1\leq N \leq 100
- 0\leq T_1< T_2< \dots < T_N\leq 10^6
- 1\leq A\leq 10^6
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A T_1 T_2 \dots T_N
出力
N 行出力せよ。 i\ (1\leq i \leq N) 行目には、i 番目の人がチケットを購入し終わるのは今から何秒後かを整数として出力せよ。
入力例 1
3 4 0 2 10
出力例 1
4 8 14
時系列順に以下のように物事が進行します。
- 0 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 2 秒後:2 番目の人がチケット売り場を訪れ、1 番目の人の後ろに並ぶ。
- 4 秒後:1 番目の人がチケットを購入し終え、2 番目の人が購入手続きを開始する。
- 8 秒後:2 番目の人がチケットを購入し終える。
- 10 秒後:3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 14 秒後:3 番目の人がチケットを購入し終える。
入力例 2
3 3 1 4 7
出力例 2
4 7 10
時系列順に以下のように物事が進行します。
- 1 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 4 秒後:1 番目の人がチケットを購入し終えると同時に、2 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 7 秒後:2 番目の人がチケットを購入し終えると同時に、3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
- 10 秒後:3 番目の人がチケットを購入し終える。
入力例 3
10 50000 120190 165111 196897 456895 540000 552614 561627 743796 757613 991216
出力例 3
170190 220190 270190 506895 590000 640000 690000 793796 843796 1041216
Score : 200 points
Problem Statement
At the entrance of AtCoder Land, there is a single ticket booth where visitors line up to purchase tickets one by one. The purchasing process takes A seconds per person. Once the person at the front of the line finishes purchasing their ticket, the next person (if any) immediately starts their purchasing process.
Currently, there is no one in line at the ticket booth, and N people will come to buy tickets one after another. Specifically, the i-th person will arrive at the ticket booth T_i seconds from now. If there is already a line, they will join the end of it; if not, they will start the purchasing process immediately. Here, T_1 < T_2 < \dots < T_N.
For each i\ (1 \leq i \leq N), determine how many seconds from now the i-th person will finish purchasing their ticket.
Constraints
- 1 \leq N \leq 100
- 0 \leq T_1 < T_2 < \dots < T_N \leq 10^6
- 1 \leq A \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A T_1 T_2 \dots T_N
Output
Print N lines. The i-th line should contain the number of seconds from now that the i-th person will finish purchasing their ticket.
Sample Input 1
3 4 0 2 10
Sample Output 1
4 8 14
The events proceed in the following order:
- At 0 seconds: The 1st person arrives at the ticket booth and starts the purchasing process.
- At 2 seconds: The 2nd person arrives at the ticket booth and joins the line behind the 1st person.
- At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person starts the purchasing process.
- At 8 seconds: The 2nd person finishes purchasing their ticket.
- At 10 seconds: The 3rd person arrives at the ticket booth and starts the purchasing process.
- At 14 seconds: The 3rd person finishes purchasing their ticket.
Sample Input 2
3 3 1 4 7
Sample Output 2
4 7 10
The events proceed in the following order:
- At 1 second: The 1st person arrives at the ticket booth and starts the purchasing process.
- At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person arrives at the ticket booth and starts the purchasing process.
- At 7 seconds: The 2nd person finishes purchasing their ticket, and the 3rd person arrives at the ticket booth and starts the purchasing process.
- At 10 seconds: The 3rd person finishes purchasing their ticket.
Sample Input 3
10 50000 120190 165111 196897 456895 540000 552614 561627 743796 757613 991216
Sample Output 3
170190 220190 270190 506895 590000 640000 690000 793796 843796 1041216
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は全 10^9 巻の漫画『すぬけ君』を読むことにしました。
初め、高橋君は『すぬけ君』の単行本を N 冊持っています。i 番目の単行本は a_i 巻です。
高橋君は『すぬけ君』を読み始める前に限り次の操作を 0 回以上何度でも繰り返せます。
- 現在持っている単行本が 1 冊以下の場合、何もしない。そうでない場合、現在持っている単行本から 2 冊を選んで売り、代わりに好きな巻を選んで 1 冊買う
その後、高橋君は『すぬけ君』を 1 巻、2 巻、3 巻、\ldots と順番に読みます。ただし、次に読むべき巻を持っていない状態になった場合、(他の巻を持っているかどうかに関わらず)その時点で『すぬけ君』を読むのをやめます。
高橋君が『すぬけ君』を最大で何巻まで読めるかを求めてください。ただし、1 巻を読めない場合の答えは 0 とします。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
6 1 2 4 6 7 271
出力例 1
4
『すぬけ君』を読み始める前に「7 巻と 271 巻を選んで売り、代わりに 3 巻を選んで 1 冊買う」という内容で操作をすると、高橋君は 1 巻、2 巻、3 巻、4 巻、6 巻を 1 冊ずつ持っている状態になります。
この状態から『すぬけ君』を読み始めると、高橋君は 1 巻、2 巻、3 巻、4 巻を読み、続いて 5 巻を読もうとしますが持っていないためこの時点で『すぬけ君』を読むのをやめます。
操作の方法を変えても 5 巻を読むことは出来ないため、4 が答えです。
入力例 2
10 1 1 1 1 1 1 1 1 1 1
出力例 2
5
高橋君は同じ巻を 2 冊以上持っているかもしれません。
この入力に対しては以下のように 4 回操作をしてから『すぬけ君』を読み始めることで 5 巻まで読むことが出来、これが最大です。
- 1 巻を 2 冊選んで売り、代わりに 2 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 3 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 4 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 5 巻を選んで 1 冊買う
入力例 3
1 5
出力例 3
0
高橋君は 1 巻を読めません。
Score : 300 points
Problem Statement
Takahashi is going to read a manga series "Snuke-kun" in 10^9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume a_i.
Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:
- Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.
Then, Takahashi reads Volume 1, Volume 2, Volume 3, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).
Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
6 1 2 4 6 7 271
Sample Output 1
4
Before he begins to read the series, he may do the following operation: "sell books of Volumes 7 and 271 and buy one book of Volume 3 instead." Then, he has one book each of Volumes 1, 2, 3, 4, and 6.
If he then begins to read the series, he reads Volumes 1, 2, 3, and 4, then tries to read Volume 5. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 5, so the answer is 4.
Sample Input 2
10 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Takahashi may have multiple books of the same volume.
For this input, if he performs the following 4 operations before he begins to read the series, he can read up to Volume 5, which is the maximum:
- Sell two books of Volume 1 and buy a book of Volume 2 instead.
- Sell two books of Volume 1 and buy a book of Volume 3 instead.
- Sell two books of Volume 1 and buy a book of Volume 4 instead.
- Sell two books of Volume 1 and buy a book of Volume 5 instead.
Sample Input 3
1 5
Sample Output 3
0
Takahashi cannot read Volume 1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。
i 番目のクエリは以下の通りです。
- 整数 X_i と文字 C_i が与えられるので、 S の X_i 番目の文字を C_i に置き換える。その後、 S に文字列
ABCが部分文字列として何箇所含まれるかを出力する。
ここで、 S の 部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ab は abc の部分文字列ですが、ac は abc の部分文字列ではありません。
制約
- 3\le N\le 2\times 10^5
- 1\le Q\le 2\times 10^5
- S は英大文字からなる長さ N の文字列
- 1\le X_i\le N
- C_i は英大文字
入力
入力は以下の形式で標準入力から与えられる。
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
出力
Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
出力例 1
2 1 1 0
各クエリを処理した後の S は次のようになります。
- 1 つ目のクエリを処理後: S=
ABCBABCとなる。この中にABCは部分文字列として 2 回登場する。 - 2 つ目のクエリを処理後: S=
ABABABCとなる。この中にABCは部分文字列として 1 回登場する。 - 3 つ目のクエリを処理後: S=
ABABCBCとなる。この中にABCは部分文字列として 1 回登場する。 - 4 つ目のクエリを処理後: S=
ABAGCBCとなる。この中にABCは部分文字列として 0 回登場する。
入力例 2
3 3 ABC 1 A 2 B 3 C
出力例 2
1 1 1
クエリの処理前と処理後で S が変わらない場合もあります。
入力例 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
出力例 3
0 0 0 0 1 1 2 2 1 1
Score : 350 points
Problem Statement
You are given a string S of length N. You are also given Q queries, which you should process in order.
The i-th query is as follows:
- Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string
ABCappears as a substring in S.
Here, 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
- 3 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- S is a string of length N consisting of uppercase English letters.
- 1 \le X_i \le N
- C_i is an uppercase English letter.
Input
The input is given from Standard Input in the following format:
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
Output
Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.
Sample Input 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
Sample Output 1
2 1 1 0
After processing each query, S becomes as follows.
- After the first query: S=
ABCBABC. In this string,ABCappears twice as a substring. - After the second query: S=
ABABABC. In this string,ABCappears once as a substring. - After the third query: S=
ABABCBC. In this string,ABCappears once as a substring. - After the fourth query: S=
ABAGCBC. In this string,ABCappears zero times as a substring.
Sample Input 2
3 3 ABC 1 A 2 B 3 C
Sample Output 2
1 1 1
There are cases where S does not change through processing a query.
Sample Input 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
Sample Output 3
0 0 0 0 1 1 2 2 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
英大文字および英小文字からなる長さ N の文字列 S が与えられます。
これから、文字列 S に対して Q 回の操作を行います。 i 番目 (1\leq i\leq Q) の操作は整数 2 つと文字 1 つからなる組 (t _ i,x _ i,c _ i) で表され、それぞれ次のような操作を表します。
- t _ i=1 のとき、S の x _ i 文字目を c _ i に変更する。
- t _ i=2 のとき、S に含まれる大文字すべてをそれぞれ小文字に変更する(x _ i,c _ i は操作に使用しない)。
- t _ i=3 のとき、S に含まれる小文字すべてをそれぞれ大文字に変更する(x _ i,c _ i は操作に使用しない)。
Q 回の操作がすべて終わったあとの S を出力してください。
制約
- 1\leq N\leq5\times10^5
- S は英大文字および英小文字からなる長さ N の文字列
- 1\leq Q\leq5\times10^5
- 1\leq t _ i\leq3\ (1\leq i\leq Q)
- t _ i=1 ならば 1\leq x _ i\leq N\ (1\leq i\leq Q)
- c _ i は英大文字もしくは英小文字
- t _ i\neq 1 ならば x _ i=0 かつ c _ i=
'a' - N,Q,t _ i,x _ i はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S Q t _ 1 x _ 1 c _ 1 t _ 2 x _ 2 c _ 2 \vdots t _ Q x _ Q c _ Q
出力
答えを 1 行で出力せよ。
入力例 1
7 AtCoder 5 1 4 i 3 0 a 1 5 b 2 0 a 1 4 Y
出力例 1
atcYber
はじめ、文字列 S は AtCoder です。
- 1 番目の操作では、4 文字目を
iに変更します。変更後の S はAtCiderです。 - 2 番目の操作では、すべての小文字を大文字に変更します。変更後の S は
ATCIDERです。 - 3 番目の操作では、5 文字目を
bに変更します。変更後の S はATCIbERです。 - 4 番目の操作では、すべての大文字を小文字に変更します。変更後の S は
atciberです。 - 5 番目の操作では、4 文字目を
Yに変更します。変更後の S はatcYberです。
すべての操作が終わったあとの S は atcYber なので、atcYber と出力してください。
入力例 2
35 TheQuickBrownFoxJumpsOverTheLazyDog 10 2 0 a 1 19 G 1 13 m 1 2 E 1 21 F 2 0 a 1 27 b 3 0 a 3 0 a 1 15 i
出力例 2
TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
Score : 400 points
Problem Statement
You are given a string S of length N consisting of uppercase and lowercase English letters.
Let us perform Q operations on the string S. The i-th operation (1\leq i\leq Q) is represented by a tuple (t _ i,x _ i,c _ i) of two integers and one character, as follows.
- If t _ i=1, change the x _ i-th character of S to c _ i.
- If t _ i=2, convert all uppercase letters in S to lowercase (do not use x _ i,c _ i for this operation).
- If t _ i=3, convert all lowercase letters in S to uppercase (do not use x _ i,c _ i for this operation).
Print the S after the Q operations.
Constraints
- 1\leq N\leq5\times10^5
- S is a string of length N consisting of uppercase and lowercase English letters.
- 1\leq Q\leq5\times10^5
- 1\leq t _ i\leq3\ (1\leq i\leq Q)
- If t _ i=1, then 1\leq x _ i\leq N\ (1\leq i\leq Q).
- c _ i is an uppercase or lowercase English letter.
- If t _ i\neq 1, then x _ i=0 and c _ i=
'a'. - N,Q,t _ i,x _ i are all integers.
Input
The input is given from Standard Input in the following format:
N S Q t _ 1 x _ 1 c _ 1 t _ 2 x _ 2 c _ 2 \vdots t _ Q x _ Q c _ Q
Output
Print the answer in a single line.
Sample Input 1
7 AtCoder 5 1 4 i 3 0 a 1 5 b 2 0 a 1 4 Y
Sample Output 1
atcYber
Initially, the string S is AtCoder.
- The first operation changes the 4-th character to
i, changing S toAtCider. - The second operation converts all lowercase letters to uppercase, changing S to
ATCIDER. - The third operation changes the 5-th character to
b, changing S toATCIbER. - The fourth operation converts all uppercase letters to lowercase, changing S to
atciber. - The fifth operation changes the 4-th character to
Y, changing S toatcYber.
After the operations, the string S is atcYber, so print atcYber.
Sample Input 2
35 TheQuickBrownFoxJumpsOverTheLazyDog 10 2 0 a 1 19 G 1 13 m 1 2 E 1 21 F 2 0 a 1 27 b 3 0 a 3 0 a 1 15 i
Sample Output 2
TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG