Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
H 行 W 列の格子状に HW 枚のカードが並べられています。
i=1,\ldots,N について、上から A_i 行目、左から B_i 列目にあるカードには数 i が書かれており、それ以外の HW-N 枚のカードには何も書かれていません。
これらのカードに対し、以下の 2 種類の操作を可能な限り繰り返します。
- 数の書かれたカードを含まない行が存在するとき、その行のカードを全て取り除き、残りのカードを上へ詰める
- 数の書かれたカードを含まない列が存在するとき、その列のカードを全て取り除き、残りのカードを左へ詰める
操作が終了したとき、数が書かれたカードがそれぞれどこにあるか求めてください。なお、答えは操作の仕方に依らず一意に定まることが証明されます。
制約
- 1 \leq H,W \leq 10^9
- 1 \leq N \leq \min(10^5,HW)
- 1 \leq A_i \leq H
- 1 \leq B_i \leq W
- (A_i,B_i) は相異なる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
H W N A_1 B_1 \vdots A_N B_N
出力
N 行出力せよ。
操作終了後に数 i が書かれたカードが上から C_i 行目、左から D_i 列目に存在するとき、i 行目には C_i,D_i をこの順に空白区切りで出力せよ。
入力例 1
4 5 2 3 2 2 5
出力例 1
2 1 1 2
何も書かれていないカードを * で表すことにします。最初、カードの配置は以下の通りです。
***** ****2 *1*** *****
操作終了後、カードの配置は以下の通りになります。
*2 1*
1 が書かれたカードは上から 2 行目、左から 1 列目にあり、2 が書かれたカードは上から 1 行目、左から 2 列目にあります。
入力例 2
1000000000 1000000000 10 1 1 10 10 100 100 1000 1000 10000 10000 100000 100000 1000000 1000000 10000000 10000000 100000000 100000000 1000000000 1000000000
出力例 2
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
Score : 300 points
Problem Statement
We have HW cards arranged in a matrix with H rows and W columns.
For each i=1, \ldots, N, the card at the A_i-th row from the top and B_i-th column from the left has a number i written on it. The other HW-N cards have nothing written on them.
We will repeat the following two kinds of operations on these cards as long as we can.
- If there is a row without a numbered card, remove all the cards in that row and fill the gap by shifting the remaining cards upward.
- If there is a column without a numbered card, remove all the cards in that column and fill the gap by shifting the remaining cards to the left.
Find the position of each numbered card after the end of the process above. It can be proved that these positions are uniquely determined without depending on the order in which the operations are done.
Constraints
- 1 \leq H,W \leq 10^9
- 1 \leq N \leq \min(10^5,HW)
- 1 \leq A_i \leq H
- 1 \leq B_i \leq W
- All pairs (A_i,B_i) are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W N A_1 B_1 \vdots A_N B_N
Output
Print N lines.
If, after the end of the process, the card with the number i is at the C_i-th row from the top and D_i-th column from the left, the i-th line should contain C_i and D_i in this order with a space between them.
Sample Input 1
4 5 2 3 2 2 5
Sample Output 1
2 1 1 2
Let * represent a card with nothing written on it. The initial arrangement of cards is:
***** ****2 *1*** *****
After the end of the process, they will be arranged as follows:
*2 1*
Here, the card with 1 is at the 2-nd row from the top and 1-st column from the left, and the card with 2 is at the 1-st row from the top and 2-nd column from the left.
Sample Input 2
1000000000 1000000000 10 1 1 10 10 100 100 1000 1000 10000 10000 100000 100000 1000000 1000000 10000000 10000000 100000000 100000000 1000000000 1000000000
Sample Output 2
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。
制約
- 入力は全て整数
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 1 5 4 9
出力例 1
2
A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。
- 例えば 2 要素目の 1 、 5 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
- このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。
入力例 2
6 5 1 1 1 1 1 1
出力例 2
0
入力例 3
8 3 31 43 26 6 18 36 22 13
出力例 3
18
Score : 250 points
Problem Statement
You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.
Constraints
- All inputs are integers.
- 1 \le K < N \le 2 \times 10^5
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 1 5 4 9
Sample Output 1
2
Consider removing exactly two elements from A=(3,1,5,4,9).
- For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
- In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.
Sample Input 2
6 5 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
8 3 31 43 26 6 18 36 22 13
Sample Output 3
18
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の英小文字列 S, T および M 個の整数の組 (L_1,R_1),(L_2,R_2),\ldots,(L_M,R_M) が与えられます。
i=1,2,\ldots,M の順に以下の操作を行います。
- S の L_i 文字目から R_i 文字目と、T の L_i 文字目から R_i 文字目を入れ替える。
- 例えば、S が
abcdef、T がghijkl、(L_i,R_i)=(3,5) のとき、操作後の S,T はそれぞれabijkf、ghcdelとなる。
- 例えば、S が
M 回の操作を行った後の S を求めてください。
制約
- 1\leq N\leq 5\times 10^5
- 1\leq M\leq 2\times 10^5
- S,T は長さ N の英小文字列
- 1\leq L_i\leq R_i\leq N
- N,M,L_i,R_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N M S T L_1 R_1 L_2 R_2 \vdots L_M R_M
出力
M 回の操作を行った後の S を出力せよ。
入力例 1
5 3 apple lemon 2 4 1 5 5 5
出力例 1
lpple
はじめ S,T はそれぞれ apple, lemon です。
- i=1 の操作後の S,T はそれぞれ
aemoe,lpplnです。 - i=2 の操作後の S,T はそれぞれ
lppln,aemoeです。 - i=3 の操作後の S,T はそれぞれ
lpple,aemonです。
よって、3 回の操作を行った後の S は lpple です。
入力例 2
10 5 lemwrbogje omsjbfggme 5 8 4 8 1 3 6 6 1 4
出力例 2
lemwrfogje
Score : 400 points
Problem Statement
You are given length-N lowercase English strings S and T, and M pairs of integers (L_1,R_1),(L_2,R_2),\ldots,(L_M,R_M).
Perform the following operation for i=1,2,\ldots,M in order:
- Swap the L_i-th through R_i-th characters of S and the L_i-th through R_i-th characters of T.
- For example, if S is
abcdef, T isghijkl, and (L_i,R_i)=(3,5), then S and T becomeabijkfandghcdel, respectively.
- For example, if S is
Find the string S after performing the M operations.
Constraints
- 1\leq N\leq 5\times 10^5
- 1\leq M\leq 2\times 10^5
- Each of S and T is a length-N lowercase English strings.
- 1\leq L_i\leq R_i\leq N
- N, M, L_i, and R_i are integers.
Input
The input is given from Standard Input in the following format:
N M S T L_1 R_1 L_2 R_2 \vdots L_M R_M
Output
Output the S after performing the M operations.
Sample Input 1
5 3 apple lemon 2 4 1 5 5 5
Sample Output 1
lpple
Initially, S and T are apple and lemon, respectively.
- After the operation for i=1, S and T are
aemoeandlppln, respectively. - After the operation for i=2, S and T are
lpplnandaemoe, respectively. - After the operation for i=3, S and T are
lppleandaemon, respectively.
Thus, the string S after the three operations is lpple.
Sample Input 2
10 5 lemwrbogje omsjbfggme 5 8 4 8 1 3 6 6 1 4
Sample Output 2
lemwrfogje
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
空の列 A があります。クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。
1 x: A の最後尾に x を追加する。2: A の最初の要素を出力する。その後、その要素を削除する。このクエリが与えられるとき、A は空でないことが保証される。3: A を昇順にソートする。
制約
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq x \leq 10^9
- クエリ
2が与えられるとき、A は空でない。 - 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q
i 番目のクエリ \mathrm{query} i では、まずクエリの種類 c_i( 1, 2, 3 のいずれか)が与えられる。 c_i = 1 の場合はさらに整数 x が追加で与えられる。
すなわち、各クエリは以下に示す 3 つの形式のいずれかである。
1 x
2
3
出力
c_i = 2 を満たすクエリの回数を q として q 行出力せよ。
j (1 \leq j \leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。
入力例 1
8 1 4 1 3 1 2 1 1 3 2 1 0 2
出力例 1
1 2
入力例 1 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。
- (4)
- (4, 3)
- (4, 3, 2)
- (4, 3, 2, 1)
- (1, 2, 3, 4)
- (2, 3, 4)
- (2, 3, 4, 0)
- (3, 4, 0)
入力例 2
9 1 5 1 5 1 3 2 3 2 1 6 3 2
出力例 2
5 3 5
入力例 2 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。
- (5)
- (5, 5)
- (5, 5, 3)
- (5, 3)
- (3, 5)
- (5)
- (5, 6)
- (5, 6)
- (6)
Score : 500 points
Problem Statement
We have an empty sequence A. You will be given Q queries, which should be processed in the order they are given. Each query is of one of the three kinds below:
1 x: Append x to the end of A.2: Print the element at the beginning of A. Then, delete that element. It is guaranteed that A will not empty when this query is given.3: Sort A in ascending order.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq x \leq 10^9
- A will not be empty when a query
2is given. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q
The i-th query, \mathrm{query} i, begins with the kind of query c_i (1, 2, or 3). If c_i = 1, the line additionally has an integer x.
In other words, each query is in one of the three formats below.
1 x
2
3
Output
Print q lines, where q is the number of queries with c_i = 2.
The j-th line (1 \leq j \leq q) should contain the response for the j-th such query.
Sample Input 1
8 1 4 1 3 1 2 1 1 3 2 1 0 2
Sample Output 1
1 2
The i-th line below shows the contents of A after the i-th query is processed in Sample Input 1.
- (4)
- (4, 3)
- (4, 3, 2)
- (4, 3, 2, 1)
- (1, 2, 3, 4)
- (2, 3, 4)
- (2, 3, 4, 0)
- (3, 4, 0)
Sample Input 2
9 1 5 1 5 1 3 2 3 2 1 6 3 2
Sample Output 2
5 3 5
The i-th line below shows the contents of A after the i-th query is processed in Sample Input 2.
- (5)
- (5, 5)
- (5, 5, 3)
- (5, 3)
- (3, 5)
- (5)
- (5, 6)
- (5, 6)
- (6)
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と正整数 M が与えられます。A の空でない連続とは限らない部分列であって、列に含まれる要素の最小公倍数が M になるようなものの個数を 998244353 で割った余りを求めてください。ただし、2 つの部分列が列として同じでも、取り出す位置が異なるならば区別するものとします。また、列の要素の個数が 1 のときはその要素自身を最小公倍数とします。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M\leq 10^{16}
- 1\leq A_i\leq 10^{16}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 6 2 3 4 6
出力例 1
5
A の部分列であって,列に含まれる要素の最小公倍数が 6 となるものは (2,3),(2,3,6),(2,6),(3,6),(6) の 5 つです。
入力例 2
5 349 1 1 1 1 349
出力例 2
16
部分列が列として同じでも、取り出す位置が異なるならば区別することに注意してください。
入力例 3
16 720720 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
出力例 3
2688
Score: 525 points
Problem Statement
You are given a sequence of positive integers A=(A_1,A_2,\dots,A_N) of length N and a positive integer M. Find the number, modulo 998244353, of non-empty and not necessarily contiguous subsequences of A such that the least common multiple (LCM) of the elements in the subsequence is M. Two subsequences are distinguished if they are taken from different positions in the sequence, even if they coincide as sequences. Also, the LCM of a sequence with a single element is that element itself.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^{16}
- 1 \leq A_i \leq 10^{16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 6 2 3 4 6
Sample Output 1
5
The subsequences of A whose elements have an LCM of 6 are (2,3),(2,3,6),(2,6),(3,6),(6); there are five of them.
Sample Input 2
5 349 1 1 1 1 349
Sample Output 2
16
Note that even if some subsequences coincide as sequences, they are distinguished if they are taken from different positions.
Sample Input 3
16 720720 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Sample Output 3
2688