実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。
1 i j
\colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる2 i
\colon 箱 i に入っているカードに書かれた数を昇順ですべて答える3 i
\colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える
ただし、以下の点に注意してください。
- 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
- 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する
制約
- 1 \leq N, Q \leq 2 \times 10^5
- 1 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- 2 番目の形式のクエリについて、
- 1 \leq i \leq N
- このクエリが与えられる時点で箱 i にカードが入っている
- 3 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
- 出力するべき数は合計で 2 \times 10^5 個以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。
1 i j
2 i
3 i
出力
2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。
入力例 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
出力例 1
1 2 1 1 2 1 4 4
クエリを順に処理していきます。
- カードに 1 を書き込み、箱 1 に入れます。
- カードに 2 を書き込み、箱 4 に入れます。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 2 です。
- 1, 2 の順に出力してください。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
- 1 を 2 度出力することに注意してください。
- 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
- 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
- 数 2 が書かれたカードが入っている箱は箱 4 です。
入力例 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
出力例 2
1 2 200000 1
Score : 300 points
Problem Statement
We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.
1 i j
\colon Write the number i on a blank card and put it into box j.2 i
\colon Report all numbers written on the cards in box i, in ascending order.3 i
\colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.
Here, note the following.
- In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
- In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- For a query of the first kind:
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- For a query of the second kind:
- 1 \leq i \leq N
- Box i contains some cards when this query is given.
- For a query of the third kind:
- 1 \leq i \leq 2 \times 10^5
- Some boxes contain a card with the number i when this query is given.
- At most 2 \times 10^5 numbers are to be reported.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:
1 i j
2 i
3 i
Output
Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.
Sample Input 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
Sample Output 1
1 2 1 1 2 1 4 4
Let us process the queries in order.
- Write 1 on a card and put it into box 1.
- Write 2 on a card and put it into box 4.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1 and 2.
- Print 1 and 2 in this order.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1, 1, and 2.
- Note that you should print 1 twice.
- Boxes 1 and 4 contain a card with the number 1.
- Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
- Boxes 4 contains a card with the number 2.
Sample Input 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
Sample Output 2
1 2 200000 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
#
と .
からなる H 行 W 列の図形 S,T が与えられます。
図形 S は H 個の文字列として与えられ、 S_i の j 文字目は S の i 行 j 列にある要素を表します。 T についても同様です。
S の列を並べ替えて T と等しくできるか判定してください。
但し、図形 X の列を並べ替えるとは、以下の操作を言います。
- (1,2,\dots,W) の順列 P=(P_1,P_2,\dots,P_W) をひとつ選択する。
- その後、全ての 1 \le i \le H を満たす整数 i について、以下の操作を同時に行う。
- 1 \le j \le W を満たす全ての整数 j について同時に、 X の i 行 j 列にある要素を i 行 P_j 列にある要素に置き換える。
制約
- H,W は整数
- 1 \le H,W
- 1 \le H \times W \le 4 \times 10^5
- S_i,T_i は
#
と.
からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
出力
S を T と等しくできるなら Yes
、 そうでないなら No
と出力せよ。
入力例 1
3 4 ##.# ##.. #... .### ..## ...#
出力例 1
Yes
例えば S の 3,4,2,1 列目をこの順に左から並べ替えた場合、 S を T と等しくできます。
入力例 2
3 3 #.# .#. #.# ##. ##. .#.
出力例 2
No
この入力では、 S を T と等しくすることができません。
入力例 3
2 1 # . # .
出力例 3
Yes
S=T である場合もあります。
入力例 4
8 7 #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. ....### ####... ....### ####... ....### ####... ....### ####...
出力例 4
Yes
Score : 300 points
Problem Statement
You are given patterns S and T consisting of #
and .
, each with H rows and W columns.
The pattern S is given as H strings, and the j-th character of S_i represents the element at the i-th row and j-th column. The same goes for T.
Determine whether S can be made equal to T by rearranging the columns of S.
Here, rearranging the columns of a pattern X is done as follows.
- Choose a permutation P=(P_1,P_2,\dots,P_W) of (1,2,\dots,W).
- Then, for every integer i such that 1 \le i \le H, simultaneously do the following.
- For every integer j such that 1 \le j \le W, simultaneously replace the element at the i-th row and j-th column of X with the element at the i-th row and P_j-th column.
Constraints
- H and W are integers.
- 1 \le H,W
- 1 \le H \times W \le 4 \times 10^5
- S_i and T_i are strings of length W consisting of
#
and.
.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H T_1 T_2 \vdots T_H
Output
If S can be made equal to T, print Yes
; otherwise, print No
.
Sample Input 1
3 4 ##.# ##.. #... .### ..## ...#
Sample Output 1
Yes
If you, for instance, arrange the 3-rd, 4-th, 2-nd, and 1-st columns of S in this order from left to right, S will be equal to T.
Sample Input 2
3 3 #.# .#. #.# ##. ##. .#.
Sample Output 2
No
In this input, S cannot be made equal to T.
Sample Input 3
2 1 # . # .
Sample Output 3
Yes
It is possible that S=T.
Sample Input 4
8 7 #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. #..#..# .##.##. ....### ####... ....### ####... ....### ####... ....### ####...
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
数列 (A_1,\ldots,A_K) を使って、高橋君と青木君が石取りゲームをします。
最初、山には N 個の石があります。高橋君から順に、二人が交互に次の操作を行います。
- 現在山にある石の個数以下であるような A_i を 1 つ選ぶ。山から A_i 個の石を取り除く。
山から石がなくなったとき、ゲームは終了します。
二人がともに、ゲーム終了までに自分が取り除いた石の個数を最大化しようとするとき、高橋君は何個の石を取り除くことができますか?
制約
- 1 \leq N \leq 10^4
- 1 \leq K \leq 100
- 1 = A_1 < A_2 < \ldots < A_K \leq N
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_K
出力
答えを出力せよ。
入力例 1
10 2 1 4
出力例 1
5
ゲームの進行の一例は以下の通りです。
- 高橋君が山から 4 個の石を取り除く。
- 青木君が山から 4 個の石を取り除く。
- 高橋君が山から 1 個の石を取り除く。
- 青木君が山から 1 個の石を取り除く。
この例では高橋君は 5 個の石を取り除くことができます。高橋君が 6 個以上の石を取り除くことはできないためこれが最大です。
高橋君は 5 個の石を取り除くことができるゲームの進行の例には、他にも次のようなものがあります。
- 高橋君が山から 1 個の石を取り除く。
- 青木君が山から 4 個の石を取り除く。
- 高橋君が山から 4 個の石を取り除く。
- 青木君が山から 1 個の石を取り除く。
入力例 2
11 4 1 2 3 6
出力例 2
8
ゲームの進行の一例は以下の通りです。
- 高橋君が山から 6 個の石を取り除く。
- 青木君が山から 3 個の石を取り除く。
- 高橋君が山から 2 個の石を取り除く。
この例では高橋君は 8 個の石を取り除くことができます。高橋君が 9 個以上の石を取り除くことはできないためこれが最大です。
入力例 3
10000 10 1 2 4 8 16 32 64 128 256 512
出力例 3
5136
Score : 400 points
Problem Statement
Takahashi and Aoki will play a game of taking stones using a sequence (A_1, \ldots, A_K).
There is a pile that initially contains N stones. The two players will alternately perform the following operation, with Takahashi going first.
- Choose an A_i that is at most the current number of stones in the pile. Remove A_i stones from the pile.
The game ends when the pile has no stones.
If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove?
Constraints
- 1 \leq N \leq 10^4
- 1 \leq K \leq 100
- 1 = A_1 < A_2 < \ldots < A_K \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_K
Output
Print the answer.
Sample Input 1
10 2 1 4
Sample Output 1
5
Below is one possible progression of the game.
- Takahashi removes 4 stones from the pile.
- Aoki removes 4 stones from the pile.
- Takahashi removes 1 stone from the pile.
- Aoki removes 1 stone from the pile.
In this case, Takahashi removes 5 stones. There is no way for him to remove 6 or more stones, so this is the maximum.
Below is another possible progression of the game where Takahashi removes 5 stones.
- Takahashi removes 1 stone from the pile.
- Aoki removes 4 stones from the pile.
- Takahashi removes 4 stones from the pile.
- Aoki removes 1 stone from the pile.
Sample Input 2
11 4 1 2 3 6
Sample Output 2
8
Below is one possible progression of the game.
- Takahashi removes 6 stones.
- Aoki removes 3 stones.
- Takahashi removes 2 stones.
In this case, Takahashi removes 8 stones. There is no way for him to remove 9 or more stones, so this is the maximum.
Sample Input 3
10000 10 1 2 4 8 16 32 64 128 256 512
Sample Output 3
5136
実行時間制限: 6 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) があり、最初全ての項が 0 です。
入力で与えられる整数 K を用いて関数 f(A) を以下のように定義します。
- A を降順に (広義単調減少となるように) ソートしたものを B とする。
- このとき、 f(A)=B_1 + B_2 + \dots + B_K とする。
この数列に合計 Q 回の更新を行うことを考えます。
数列 A に対し以下の更新を i=1,2,\dots,Q の順に行い、各更新ごとにその時点での f(A) の値を出力してください。
- A_{X_i} を Y_i に変更する。
制約
- 入力は全て整数
- 1 \le K \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le X_i \le N
- 0 \le Y_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
出力
全体で Q 行出力せよ。そのうち i 行目には i 回目の更新を終えた時点での f(A) の値を整数として出力せよ。
入力例 1
4 2 10 1 5 2 1 3 3 4 2 2 10 1 0 4 0 3 1 2 0 3 0
出力例 1
5 6 8 8 15 13 13 11 1 0
この入力では N=4,K=2 です。 Q=10 回の更新を行います。
- 1 回目の更新を受けて A=(5,0,0,0) となります。このとき f(A)=5 です。
- 2 回目の更新を受けて A=(5,1,0,0) となります。このとき f(A)=6 です。
- 3 回目の更新を受けて A=(5,1,3,0) となります。このとき f(A)=8 です。
- 4 回目の更新を受けて A=(5,1,3,2) となります。このとき f(A)=8 です。
- 5 回目の更新を受けて A=(5,10,3,2) となります。このとき f(A)=15 です。
- 6 回目の更新を受けて A=(0,10,3,2) となります。このとき f(A)=13 です。
- 7 回目の更新を受けて A=(0,10,3,0) となります。このとき f(A)=13 です。
- 8 回目の更新を受けて A=(0,10,1,0) となります。このとき f(A)=11 です。
- 9 回目の更新を受けて A=(0,0,1,0) となります。このとき f(A)=1 です。
- 10 回目の更新を受けて A=(0,0,0,0) となります。このとき f(A)=0 です。
Score : 475 points
Problem Statement
We have a sequence A=(A_1,A_2,\dots,A_N) of length N. Initially, all the terms are 0.
Using an integer K given in the input, we define a function f(A) as follows:
- Let B be the sequence obtained by sorting A in descending order (so that it becomes monotonically non-increasing).
- Then, let f(A)=B_1 + B_2 + \dots + B_K.
We consider applying Q updates on this sequence.
Apply the following operation on the sequence A for i=1,2,\dots,Q in this order, and print the value f(A) at that point after each update.
- Change A_{X_i} to Y_i.
Constraints
- All input values are integers.
- 1 \le K \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le X_i \le N
- 0 \le Y_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
Output
Print Q lines in total. The i-th line should contain the value f(A) as an integer when the i-th update has ended.
Sample Input 1
4 2 10 1 5 2 1 3 3 4 2 2 10 1 0 4 0 3 1 2 0 3 0
Sample Output 1
5 6 8 8 15 13 13 11 1 0
In this input, N=4 and K=2. Q=10 updates are applied.
- The 1-st update makes A=(5, 0,0,0). Now, f(A)=5.
- The 2-nd update makes A=(5, 1,0,0). Now, f(A)=6.
- The 3-rd update makes A=(5, 1,3,0). Now, f(A)=8.
- The 4-th update makes A=(5, 1,3,2). Now, f(A)=8.
- The 5-th update makes A=(5,10,3,2). Now, f(A)=15.
- The 6-th update makes A=(0,10,3,2). Now, f(A)=13.
- The 7-th update makes A=(0,10,3,0). Now, f(A)=13.
- The 8-th update makes A=(0,10,1,0). Now, f(A)=11.
- The 9-th update makes A=(0, 0,1,0). Now, f(A)=1.
- The 10-th update makes A=(0, 0,0,0). Now, f(A)=0.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
Q 個のクエリを処理してください。 i\ (1\leq i\leq Q) 番目のクエリは以下の通りです:
- 整数 R_i,X_i が与えられる。数列 (A_1,A_2,\dots,A_{R_i}) の(連続とは限らない)部分列であって、狭義単調増加であり、かつ全ての要素が X_i 以下であるようなものの長さの最大値を求めよ。 なお、X_i \geq \min\lbrace A_1, A_2,\dots,A_{R_i} \rbrace が保証される。
制約
- 1\leq N,Q \leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 1\leq R_i\leq N
- \min\lbrace A_1, A_2,\dots,A_{R_i} \rbrace\leq X_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \dots A_N R_1 X_1 R_2 X_2 \vdots R_Q X_Q
出力
Q 行出力せよ。 i\ (1\leq i \leq Q) 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
5 3 2 4 1 3 3 2 5 5 2 5 3
出力例 1
2 1 2
- 1 番目のクエリ:数列 (2,4) の狭義単調増加な部分列であって、全ての要素が 5 以下であるようなものの長さの最大値は 2 です。
具体的には、部分列 (2,4) が該当します。 - 2 番目のクエリ:数列 (2,4,1,3,3) の狭義単調増加な部分列であって、全ての要素が 2 以下であるようなものの長さの最大値は 1 です。
具体的には、部分列 (2) および (1) が該当します。 - 3 番目のクエリ:数列 (2,4,1,3,3) の狭義単調増加な部分列であって、全ての要素が 3 以下であるようなものの長さの最大値は 2 です。
具体的には、部分列 (2,3) および (1,3) が該当します。
入力例 2
10 8 2 5 6 5 2 1 7 9 7 2 7 8 5 2 2 3 2 6 7 3 8 9 9 6 8 7
出力例 2
4 1 1 2 1 5 3 4
Score : 500 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N.
Answer Q queries. The i-th query (1 \leq i \leq Q) is as follows:
- You are given integers R_i and X_i. Consider a subsequence (not necessarily contiguous) of (A_1, A_2, \dots, A_{R_i}) that is strictly increasing and consists only of elements at most X_i. Find the maximum possible length of such a subsequence. It is guaranteed that X_i \geq \min\lbrace A_1, A_2,\dots,A_{R_i} \rbrace.
Constraints
- 1 \leq N,Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq R_i \leq N
- \min\lbrace A_1, A_2,\dots,A_{R_i} \rbrace\leq X_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q A_1 A_2 \dots A_N R_1 X_1 R_2 X_2 \vdots R_Q X_Q
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Sample Input 1
5 3 2 4 1 3 3 2 5 5 2 5 3
Sample Output 1
2 1 2
- 1st query: For the sequence (2,4), the longest strictly increasing subsequence with all elements at most 5 has length 2.
Specifically, (2,4) qualifies. - 2nd query: For the sequence (2,4,1,3,3), the longest strictly increasing subsequence with all elements at most 2 has length 1.
Specifically, (2) and (1) qualify. - 3rd query: For the sequence (2,4,1,3,3), the longest strictly increasing subsequence with all elements at most 3 has length 2.
Specifically, (2,3) and (1,3) qualify.
Sample Input 2
10 8 2 5 6 5 2 1 7 9 7 2 7 8 5 2 2 3 2 6 7 3 8 9 9 6 8 7
Sample Output 2
4 1 1 2 1 5 3 4