Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数の多重集合 S があります。はじめ S は空です。
Q 個のクエリが与えられるので順に処理してください。 クエリは次の 3 種類のいずれかです。
-
1 x: S に x を 1 個追加する。 -
2 x c: S から x を \mathrm{min}(c, (S に含まれる x の個数 )) 個削除する。 -
3: (S の最大値 )- (S の最小値 ) を出力する。このクエリを処理するとき、 S が空でないことが保証される。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
3のクエリを処理するとき、S は空でない。- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
i 番目のクエリを表す \mathrm{query}_i は以下の 3 種類のいずれかである。
1 x
2 x c
3
出力
3 のクエリに対する答えを順に改行区切りで出力せよ。
入力例 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
出力例 1
1 5 4
多重集合 S は以下のように変化します。
- 1 番目のクエリ : S に 3 を追加する。S は \lbrace3 \rbrace となる。
- 2 番目のクエリ : S に 2 を追加する。S は \lbrace2, 3\rbrace となる。
- 3 番目のクエリ : S = \lbrace 2, 3\rbrace の最大値は 3 、最小値は 2 なので、 3-2=1 を出力する。
- 4 番目のクエリ : S に 2 を追加する。S は \lbrace2,2,3 \rbrace となる。
- 5 番目のクエリ : S に 7 を追加する。S は \lbrace2, 2,3, 7\rbrace となる。
- 6 番目のクエリ : S = \lbrace2,2,3, 7\rbrace の最大値は 7 、最小値は 2 なので、 7-2=5 を出力する。
- 7 番目のクエリ : S に含まれる 2 の個数は 2 個なので、 \mathrm{min(2,3)} = 2 個の 2 を S から削除する。S は \lbrace3, 7\rbrace となる。
- 8 番目のクエリ : S = \lbrace3, 7\rbrace の最大値は 7 、最小値は 3 なので、 7-3=4 を出力する。
入力例 2
4 1 10000 1 1000 2 100 3 1 10
出力例 2
クエリ 3 が含まれない場合、何も出力してはいけません。
Score : 300 points
Problem Statement
We have a multiset of integers S, which is initially empty.
Given Q queries, process them in order. Each query is of one of the following types.
-
1 x: Insert an x into S. -
2 x c: Remove an x from S m times, where m = \mathrm{min}(c,( the number of x's contained in S)). -
3: Print ( maximum value of S)-( minimum value of S). It is guaranteed that S is not empty when this query is given.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
- When a query of type
3is given, S is not empty. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
\mathrm{query}_i, which denotes the i-th query, is in one of the following formats:
1 x
2 x c
3
Output
Print the answers for the queries of type 3 in the given order, separated by newlines.
Sample Input 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
Sample Output 1
1 5 4
The multiset S transitions as follows.
- 1-st query: insert 3 into S. S is now \lbrace 3 \rbrace.
- 2-nd query: insert 2 into S. S is now \lbrace 2, 3 \rbrace.
- 3-rd query: the maximum value of S = \lbrace 2, 3\rbrace is 3 and its minimum value is 2, so print 3-2=1.
- 4-th query: insert 2 into S. S is now \lbrace 2,2,3 \rbrace.
- 5-th query: insert 7 into S. S is now \lbrace 2, 2,3, 7\rbrace.
- 6-th query: the maximum value of S = \lbrace 2,2,3, 7\rbrace is 7 and its minimum value is 2, so print 7-2=5.
- 7-th query: since there are two 2's in S and \mathrm{min(2,3)} = 2, remove 2 from S twice. S is now \lbrace 3, 7\rbrace.
- 8-th query: the maximum value of S = \lbrace 3, 7\rbrace is 7 and its minimum value is 3, so print 7-3=4.
Sample Input 2
4 1 10000 1 1000 2 100 3 1 10
Sample Output 2
If the given queries do not contain that of type 3, nothing should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。
高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。
- 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。
操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q x_1 \vdots x_Q
出力
a_1,\ldots,a_N を空白区切りで出力せよ。
入力例 1
5 5 1 2 3 4 5
出力例 1
1 2 3 5 4
操作は以下のように行われます。
- 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
- 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
- 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。
入力例 2
7 7 7 7 7 7 7 7 7
出力例 2
1 2 3 4 5 7 6
入力例 3
10 6 1 5 2 9 6 6
出力例 3
1 2 3 4 5 7 6 8 10 9
Score : 300 points
Problem Statement
N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.
Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.
- Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.
Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q x_1 \vdots x_Q
Output
Print a_1,\ldots,a_N, with spaces in between.
Sample Input 1
5 5 1 2 3 4 5
Sample Output 1
1 2 3 5 4
The operations are performed as follows.
- Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
- Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
- Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.
Sample Input 2
7 7 7 7 7 7 7 7 7
Sample Output 2
1 2 3 4 5 7 6
Sample Input 3
10 6 1 5 2 9 6 6
Sample Output 3
1 2 3 4 5 7 6 8 10 9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
非負整数 x に対し定義される関数 f(x) は以下の条件を満たします。
- f(0) = 1
- 任意の正整数 k に対し f(k) = f(\lfloor \frac{k}{2}\rfloor) + f(\lfloor \frac{k}{3}\rfloor)
ここで、\lfloor A \rfloor は A の小数点以下を切り捨てた値を指します。
このとき、 f(N) を求めてください。
制約
- N は 0 \le N \le 10^{18} を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
2
出力例 1
3
f(2) = f(\lfloor \frac{2}{2}\rfloor) + f(\lfloor \frac{2}{3}\rfloor) = f(1) + f(0) =(f(\lfloor \frac{1}{2}\rfloor) + f(\lfloor \frac{1}{3}\rfloor)) + f(0) =(f(0)+f(0)) + f(0)= 3 です。
入力例 2
0
出力例 2
1
入力例 3
100
出力例 3
55
Score : 400 points
Problem Statement
A function f(x) defined for non-negative integers x satisfies the following conditions.
- f(0) = 1.
- f(k) = f(\lfloor \frac{k}{2}\rfloor) + f(\lfloor \frac{k}{3}\rfloor) for any positive integer k.
Here, \lfloor A \rfloor denotes the value of A rounded down to an integer.
Find f(N).
Constraints
- N is an integer satisfying 0 \le N \le 10^{18}.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
2
Sample Output 1
3
We have f(2) = f(\lfloor \frac{2}{2}\rfloor) + f(\lfloor \frac{2}{3}\rfloor) = f(1) + f(0) =(f(\lfloor \frac{1}{2}\rfloor) + f(\lfloor \frac{1}{3}\rfloor)) + f(0) =(f(0)+f(0)) + f(0)= 3.
Sample Input 2
0
Sample Output 2
1
Sample Input 3
100
Sample Output 3
55
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点は 1, \dots, N と番号付けられ、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i, V_i を結んでいます。
それぞれの頂点を赤または青で塗る方法は全部で 2^N 通りあります。これらのうち、以下の条件を全て満たすものの総数を 998244353 で割った余りを求めてください。
- 赤く塗られた頂点がちょうど K 個ある
- 異なる色で塗られた頂点を結ぶ辺の本数は偶数である
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K U_1 V_1 \vdots U_M V_M
出力
答えを出力せよ。
入力例 1
4 4 2 1 2 1 3 2 3 3 4
出力例 1
2
以下の 2 通りが条件を満たします。
- 頂点 1, 2 を赤く、頂点 3, 4 を青く塗る。
- 頂点 3, 4 を赤く、頂点 1, 2 を青く塗る。
上記の塗り方について、異なる色で塗られた頂点を結ぶ辺は 2 番目の辺と 3 番目の辺です。
入力例 2
10 10 3 1 2 2 4 1 5 3 6 3 9 4 10 7 8 9 10 5 9 3 4
出力例 2
64
Score : 500 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertices U_i and V_i.
There are 2^N ways to paint each vertex red or blue. Find the number, modulo 998244353, of such ways that satisfy all of the following conditions:
- There are exactly K vertices painted red.
- There is an even number of edges connecting vertices painted in different colors.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
- (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K U_1 V_1 \vdots U_M V_M
Output
Print the answer.
Sample Input 1
4 4 2 1 2 1 3 2 3 3 4
Sample Output 1
2
The following two ways satisfy the conditions.
- Paint Vertices 1 and 2 red and Vertices 3 and 4 blue.
- Paint Vertices 3 and 4 red and Vertices 1 and 2 blue.
In either of the ways above, the 2-nd and 3-rd edges connect vertices painted in different colors.
Sample Input 2
10 10 3 1 2 2 4 1 5 3 6 3 9 4 10 7 8 9 10 5 9 3 4
Sample Output 2
64
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2 次元平面上に N 頂点 0 辺のグラフ G があります。頂点には 1 から N までの番号が付いており、頂点 i は座標 (x_i,y_i) にあります。
G の頂点 u,v に対して、u,v の距離 d(u,v) をマンハッタン距離 d(u,v)=|x_u-x_v|+|y_u-y_v| で定義します。
また、G の 2 つの連結成分 A,B に対して、A,B の頂点集合をそれぞれ V(A),V(B) としたとき、A,B の距離 d(A,B) を d(A,B)=\min\lbrace d(u,v)\mid u \in V(A), v\in V(B)\rbrace で定義します。
以下で説明されるクエリを Q 個処理してください。クエリは次の 3 種類のいずれかです。
1 a b: G の頂点数を n としたとき、頂点 n+1 の座標を (x_{n+1},y_{n+1})=(a,b) として、G に頂点 n+1 を追加する。2: G の頂点数を n、連結成分数を m とする。- m=1 のとき、
-1を出力する。 - m\geq 2 のとき、距離が最小である連結成分をすべてマージし、その最小の距離の値を出力する。厳密には、G の連結成分を A_1,A_2,\ldots,A_m として、\displaystyle k=\min_{1\leq i\lt j\leq m} d(A_i,A_j) とする。同じ連結成分にない頂点の組 (u,v)\ (1\leq u\lt v\leq n) であって d(u,v)=k を満たすようなものすべてについて、頂点 u と v の間に辺を張る。その後、k を出力する。
- m=1 のとき、
3 u v: 頂点 u,v が同じ連結成分にあるならばYesを、そうでないならばNoを出力する。
制約
- 2\leq N\leq 1500
- 1\leq Q\leq 1500
- 0\leq x_i,y_i\leq 10^9
- 1 種類目のクエリについて、0\leq a,b\leq 10^9
- 3 種類目のクエリについて、そのクエリを処理する直前の G の頂点数を n としたとき、1\leq u\lt v\leq n
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。\mathrm{query}_i は i 番目に処理するクエリである。
N Q
x_1 y_1
x_2 y_2
\vdots
x_N y_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の 3 種類のいずれかの形式で与えられる。
1 a b
2
3 u v
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
4 11 3 4 3 3 7 3 2 2 3 1 2 2 3 1 2 1 6 4 2 3 2 5 2 3 2 5 2 1 2 2 2
出力例 1
No 1 Yes 2 No 3 Yes -1 0
はじめ、頂点 1,2,3,4 はそれぞれ座標 (3,4),(3,3),(7,3),(2,2) にあります。
- 1 番目のクエリについて、頂点 1 と 2 は連結でないため、
Noを出力します。 - 2 番目のクエリについて、連結成分は 4 つあり、各連結成分に含まれる頂点集合は \lbrace 1\rbrace, \lbrace 2\rbrace, \lbrace 3\rbrace, \lbrace 4\rbrace です。異なる連結成分間の距離の最小値は 1 であり、頂点 1 と 2 の間に辺が張られます。1 を出力します。
- 3 番目のクエリについて、頂点 1 と 2 は連結であるため、
Yesを出力します。 - 4 番目のクエリについて、座標 (6,4) に頂点 5 を追加します。
- 5 番目のクエリについて、連結成分は 4 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2\rbrace,\lbrace 3\rbrace,\lbrace 4\rbrace,\lbrace 5\rbrace です。異なる連結成分間の距離の最小値は 2 であり、頂点 2 と 4 の間および頂点 3 と 5 の間に辺が張られます。2 を出力します。
- 6 番目のクエリについて、頂点 2 と 5 は連結でないため、
Noを出力します。 - 7 番目のクエリについて、連結成分は 2 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2,4\rbrace,\lbrace 3,5\rbrace です。異なる連結成分間の距離の最小値は 3 であり、頂点 1 と 5 の間に辺が張られます。3 を出力します。
- 8 番目のクエリについて、頂点 2 と 5 は連結であるため、
Yesを出力します。 - 9 番目のクエリについて、連結成分は 1 つであるため -1 を出力します。
- 10 番目のクエリについて、座標 (2,2) に頂点 6 を追加します。
- 11 番目のクエリについて、連結成分は 2 つあり、各連結成分に含まれる頂点集合は \lbrace 1,2,3,4,5\rbrace,\lbrace 6\rbrace です。異なる連結成分間の距離の最小値は 0 であり、頂点 4 と 6 の間に辺が張られます。0 を出力します。
Score : 500 points
Problem Statement
There is a graph G with N vertices and 0 edges on a 2-dimensional plane. The vertices are numbered from 1 to N, and vertex i is located at coordinates (x_i,y_i).
For vertices u and v of G, the distance d(u,v) between u and v is defined as the Manhattan distance d(u,v)=|x_u-x_v|+|y_u-y_v|.
Also, for two connected components A and B of G, let V(A) and V(B) be the vertex sets of A and B, respectively. The distance d(A,B) between A and B is defined as d(A,B)=\min\lbrace d(u,v)\mid u \in V(A), v\in V(B)\rbrace.
Process Q queries as described below. Each query is one of the following three types:
1 a b: Let n be the number of vertices in G. Add vertex n+1 to G with coordinates (x_{n+1},y_{n+1})=(a,b).2: Let n be the number of vertices in G and m be the number of connected components.- If m=1, output
-1. - If m\geq 2, merge all connected components with the minimum distance and output the value of that minimum distance. Formally, let the connected components of G be A_1,A_2,\ldots,A_m and let \displaystyle k=\min_{1\leq i\lt j\leq m} d(A_i,A_j). For all pairs of vertices (u,v)\ (1\leq u\lt v\leq n) that are not in the same connected component and satisfy d(u,v)=k, add an edge between vertices u and v. Then, output k.
- If m=1, output
3 u v: If vertices u and v are in the same connected component, outputYes; otherwise, outputNo.
Constraints
- 2\leq N\leq 1500
- 1\leq Q\leq 1500
- 0\leq x_i,y_i\leq 10^9
- For queries of type 1, 0\leq a,b\leq 10^9.
- For queries of type 3, let n be the number of vertices in G just before processing that query, then 1\leq u\lt v\leq n.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{query}_i is the i-th query to be processed.
N Q
x_1 y_1
x_2 y_2
\vdots
x_N y_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in one of the following three formats:
1 a b
2
3 u v
Output
Output the answers to the queries separated by newlines, following the instructions in the problem statement.
Sample Input 1
4 11 3 4 3 3 7 3 2 2 3 1 2 2 3 1 2 1 6 4 2 3 2 5 2 3 2 5 2 1 2 2 2
Sample Output 1
No 1 Yes 2 No 3 Yes -1 0
Initially, vertices 1,2,3,4 are located at coordinates (3,4),(3,3),(7,3),(2,2), respectively.
- For the 1st query, vertices 1 and 2 are not connected, so output
No. - For the 2nd query, there are 4 connected components, and the vertex set of each connected component is \lbrace 1\rbrace, \lbrace 2\rbrace, \lbrace 3\rbrace, \lbrace 4\rbrace. The minimum distance between different connected components is 1, and an edge is added between vertices 1 and 2. Output 1.
- For the 3rd query, vertices 1 and 2 are connected, so output
Yes. - For the 4th query, add vertex 5 at coordinates (6,4).
- For the 5th query, there are 4 connected components, and the vertex set of each connected component is \lbrace 1,2\rbrace,\lbrace 3\rbrace,\lbrace 4\rbrace,\lbrace 5\rbrace. The minimum distance between different connected components is 2, and edges are added between vertices 2 and 4 and between vertices 3 and 5. Output 2.
- For the 6th query, vertices 2 and 5 are not connected, so output
No. - For the 7th query, there are 2 connected components, and the vertex set of each connected component is \lbrace 1,2,4\rbrace,\lbrace 3,5\rbrace. The minimum distance between different connected components is 3, and an edge is added between vertices 1 and 5. Output 3.
- For the 8th query, vertices 2 and 5 are connected, so output
Yes. - For the 9th query, there is 1 connected component, so output -1.
- For the 10th query, add vertex 6 at coordinates (2,2).
- For the 11th query, there are 2 connected components, and the vertex set of each connected component is \lbrace 1,2,3,4,5\rbrace,\lbrace 6\rbrace. The minimum distance between different connected components is 0, and an edge is added between vertices 4 and 6. Output 0.