Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
長さ N の整数列 A,B が与えられます。1 以上 N 以下の整数 i,j を選んで、 A_i + B_j の値を最大化してください。
制約
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9\,(i=1,2,\dots,N)
- |B_j| \leq 10^9\,(j=1,2,\dots,N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
A_i + B_j の最大値を出力せよ。
入力例 1
2 -1 5 3 -7
出力例 1
8
(i,j)=(1,1),(1,2),(2,1),(2,2) に対する A_i+B_j の値はそれぞれ 2,-8,8,-2 であり、(i,j)=(2,1) が最大値 8 を達成します。
入力例 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
出力例 2
33
Score : 250 points
Problem Statement
You are given two integer sequences A and B, each of length N. Choose integers i, j (1 \leq i, j \leq N) to maximize the value of A_i + B_j.
Constraints
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9 (i=1,2,\dots,N)
- |B_j| \leq 10^9 (j=1,2,\dots,N)
- 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 B_1 B_2 \dots B_N
Output
Print the maximum possible value of A_i + B_j.
Sample Input 1
2 -1 5 3 -7
Sample Output 1
8
For (i,j) = (1,1), (1,2), (2,1), (2,2), the values of A_i + B_j are 2, -8, 8, -2 respectively, and (i,j) = (2,1) achieves the maximum value 8.
Sample Input 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
Sample Output 2
33
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
人 1, 人 2,\ldots, 人 N の N 人が一列に並んでいます。
並び方の情報が長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) として与えられます。
A _ i\ (1\leq i\leq N) は次のような情報を表しています。
- A _ i=-1 のとき、人 i は先頭に並んでいる。
- A _ i\neq -1 のとき、人 i は人 A _ i のすぐ後ろに並んでいる。
列に並んでいる人の番号を先頭から順番に出力してください。
制約
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 もしくは 1\leq A _ i\leq N\ (1\leq i\leq N)
- 情報と矛盾しないような N 人の並び方がただ 1 通り存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
人 s _ 1, 人 s _ 2,\ldots, 人 s _ N がこの順に列に並んでいるとき、s _ 1,s _ 2,\ldots,s _ N をこの順に空白を区切りとして出力せよ。
入力例 1
6 4 1 -1 5 3 2
出力例 1
3 5 4 1 2 6
先頭から、人 3, 人 5, 人 4, 人 1, 人 2, 人 6 がこの順に列に並んでいるとき、与えられた情報と一致しています。
実際、
- 人 1 は人 4 のすぐ後ろに並んでいます。
- 人 2 は人 1 のすぐ後ろに並んでいます。
- 人 3 は先頭に並んでいます。
- 人 4 は人 5 のすぐ後ろに並んでいます。
- 人 5 は人 3 のすぐ後ろに並んでいます。
- 人 6 は人 2 のすぐ後ろに並んでいます。
となり、与えられた情報と一致していることが確認できます。
よって、3,5,4,1,2,6 をこの順に空白区切りで出力してください。
入力例 2
10 -1 1 2 3 4 5 6 7 8 9
出力例 2
1 2 3 4 5 6 7 8 9 10
入力例 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
出力例 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Score: 300 points
Problem Statement
There are N people standing in a line: person 1, person 2, \ldots, person N.
You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
A _ i\ (1\leq i\leq N) represents the following information:
- if A _ i=-1, person i is at the front of the line;
- if A _ i\neq -1, person i is right behind person A _ i.
Print the people's numbers in the line from front to back.
Constraints
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
- There is exactly one way to arrange the N people consistent with the information given.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.
Sample Input 1
6 4 1 -1 5 3 2
Sample Output 1
3 5 4 1 2 6
If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.
Indeed, it can be seen that:
- person 1 is standing right behind person 4,
- person 2 is standing right behind person 1,
- person 3 is at the front of the line,
- person 4 is standing right behind person 5,
- person 5 is standing right behind person 3, and
- person 6 is standing right behind person 2.
Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.
Sample Input 2
10 -1 1 2 3 4 5 6 7 8 9
Sample Output 2
1 2 3 4 5 6 7 8 9 10
Sample Input 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
Sample Output 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ L メートルの直線状の木材があります。
x = 1, 2, \dots, L - 1 に対して、木材の左端から x メートルの地点には目印として線 x が引かれています。
Q 個のクエリが与えられます。 i 番目のクエリは数の組 (c_i, x_i) によって表されます。
以下の説明に従ってクエリを i の昇順に処理してください。
- c_i = 1 のとき : 線 x_i がある地点で木材を 2 つに切る。
- c_i = 2 のとき : 線 x_i を含む木材を選び、その長さを出力する。
ただし c_i = 1, 2 の両方に対して、線 x_i はクエリを処理する時点で切られていないことが保証されます。
制約
- 1 \leq L \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- c_i = 1, 2 (1 \leq i \leq Q)
- 1 \leq x_i \leq L - 1 (1 \leq i \leq Q)
- 全ての i (1 \leq i \leq Q) に対して次が成り立つ: 1 \leq j \lt i かつ (c_j,x_j) = (1, x_i) を満たす j は存在しない。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
L Q c_1 x_1 c_2 x_2 \vdots c_Q x_Q
出力
c_i = 2 を満たすクエリの回数と等しい行数だけ出力せよ。 j 行目では j 番目のそのようなクエリに対する答えを出力せよ。
入力例 1
5 3 2 2 1 3 2 2
出力例 1
5 3
1 番目のクエリ時点では木材は一度も切られていないので、線 2 を含む木材の長さは 5 メートルです。よって 5 を出力します。
2 番目のクエリによって、木材は 3 メートルの木材と 2 メートルの木材に分割されます。
3 番目のクエリ時点では 線 2 を含む木材の長さは 3 メートルなので、3 を出力します。
入力例 2
5 3 1 2 1 4 2 3
出力例 2
2
入力例 3
100 10 1 31 2 41 1 59 2 26 1 53 2 58 1 97 2 93 1 23 2 84
出力例 3
69 31 6 38 38
Score : 400 points
Problem Statement
We have a long piece of timber with a length of L meters.
For each x = 1, 2, \dots, L - 1, there is a mark called Mark x at x meters from the left end of the piece.
You are given Q queries, the i-th of which is represented as a pair of numbers (c_i, x_i).
Process the queries in ascending order of i as described below.
- If c_i = 1: cut the piece at Mark x_i into two.
- If c_i = 2: choose the piece with Mark x_i on it and print its length.
Here, for both kinds of queries c_i = 1, 2, it is guaranteed that there will have been no cut at Mark x_i when the query is to be processed.
Constraints
- 1 \leq L \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- c_i = 1, 2 (1 \leq i \leq Q)
- 1 \leq x_i \leq L - 1 (1 \leq i \leq Q)
- For every i (1 \leq i \leq Q), the following holds: there is no j such that 1 \leq j \lt i and (c_j,x_j) = (1, x_i).
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
L Q c_1 x_1 c_2 x_2 \vdots c_Q x_Q
Output
Print the number of lines equal to the number of queries c_i = 2. In the j-th line, print the response to the j-th such query.
Sample Input 1
5 3 2 2 1 3 2 2
Sample Output 1
5 3
At the time of the first query, no cut has been made, so the piece with Mark 2 has a length of 5 meters. Thus, you should print 5.
In the second query, the piece is cut into two pieces with lengths of 3 and 2 meters.
At the time of the third query, the piece with Mark 2 has a length of 3 meters, so you should print 3.
Sample Input 2
5 3 1 2 1 4 2 3
Sample Output 2
2
Sample Input 3
100 10 1 31 2 41 1 59 2 26 1 53 2 58 1 97 2 93 1 23 2 84
Sample Output 3
69 31 6 38 38
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の根付き木があります。頂点には 1 から N の番号がついており、根は頂点 1 です。
i 番目の辺は頂点 A_i と B_i を結んでいます。
頂点 i には整数 X_i が書かれています。
Q 個のクエリが与えられます。i 番目のクエリでは整数の組 (V_i,K_i) が与えられるので、次の問題に答えてください。
- 問題:頂点 V_i の部分木に含まれる頂点に書かれた整数のうち、大きい方から K_i 番目の値を求めよ
制約
- 2 \leq N \leq 10^5
- 0\leq X_i\leq 10^9
- 1\leq A_i,B_i\leq N
- 1\leq Q \leq 10^5
- 1\leq V_i\leq N
- 1\leq K_i\leq 20
- 与えられるグラフは木である
- 頂点 V_i の部分木は頂点を K_i 個以上持つ
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N Q
X_1 \ldots X_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 K_1
\vdots
V_Q K_Q
出力
Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
5 2 1 2 3 4 5 1 4 2 1 2 5 3 2 1 2 2 1
出力例 1
4 5
この入力において与えられる木は下図のようなものです。

1 番目のクエリでは、頂点 1 の部分木に含まれる頂点 1,2,3,4,5 に書かれた数のうち大きい方から 2 番目である 4 を出力します。
2 番目のクエリでは、頂点 2 の部分木に含まれる頂点 2,3,5 に書かれた数のうち大きい方から 1 番目である 5 を出力します。
入力例 2
6 2 10 10 10 9 8 8 1 4 2 1 2 5 3 2 6 4 1 4 2 2
出力例 2
9 10
入力例 3
4 4 1 10 100 1000 1 2 2 3 3 4 1 4 2 3 3 2 4 1
出力例 3
1 10 100 1000
Score : 500 points
Problem Statement
We have a rooted tree with N vertices. The vertices are numbered 1 through N, and the root is Vertex 1.
The i-th edge connects Vertices A_i and B_i.
Vertex i has an integer X_i written on it.
You are given Q queries. For the i-th query, given a pair of integers (V_i,K_i), answer the following question.
- Question: among the integers written on the vertices in the subtree rooted at Vertex V_i, find the K_i-th largest value.
Constraints
- 2 \leq N \leq 10^5
- 0\leq X_i\leq 10^9
- 1\leq A_i,B_i\leq N
- 1\leq Q \leq 10^5
- 1\leq V_i\leq N
- 1\leq K_i\leq 20
- The given graph is a tree.
- The subtree rooted at Vertex V_i has K_i or more vertices.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
X_1 \ldots X_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 K_1
\vdots
V_Q K_Q
Output
Print Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
5 2 1 2 3 4 5 1 4 2 1 2 5 3 2 1 2 2 1
Sample Output 1
4 5
The tree given in this input is shown below.

For the 1-st query, the vertices in the subtree rooted at Vertex 1 are Vertices 1, 2, 3, 4, and 5, so print the 2-nd largest value of the numbers written on these vertices, 4.
For the 2-nd query, the vertices in the subtree rooted at Vertex 2 are Vertices 2, 3, and 5, so print the 1-st largest value of the numbers written on these vertices, 5.
Sample Input 2
6 2 10 10 10 9 8 8 1 4 2 1 2 5 3 2 6 4 1 4 2 2
Sample Output 2
9 10
Sample Input 3
4 4 1 10 100 1000 1 2 2 3 3 4 1 4 2 3 3 2 4 1
Sample Output 3
1 10 100 1000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
整数 K が与えられます。はじめ空である集合 S に対して、次の 2 種類のクエリを順に Q 個処理してください。
1 x:整数 x が与えられる。S に x が含まれているならば、S から x を取り除く。そうでないならば、S に x を追加する。2 x:S に含まれる整数 x が与えられる。S に含まれる数を頂点とし、差の絶対値が K 以下であるような数の間に辺を張ったグラフにおいて、x が属する連結成分の頂点数を出力する。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq K \leq 10^{18}
- 各クエリにおいて、1\leq x \leq 10^{18}
- 2 種類目のクエリにおいて与えられる x はその時点で S に含まれる。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
Q K
\mathrm{query}_1
\vdots
\mathrm{query}_Q
各クエリはそれぞれ次の形式で与えられる。
1 x
2 x
出力
クエリを処理せよ。
入力例 1
7 5 1 3 1 10 2 3 1 7 2 3 1 10 2 3
出力例 1
1 3 2
クエリの処理は次のように進行します。
- 1 番目のクエリでは、S に 3 が追加され、S=\{3\} となります。
- 2 番目のクエリでは、S に 10 が追加され、S=\{3,10\} となります。
- 3 番目のクエリでは、3,10 の 2 頂点からなる辺のないグラフを考え、3 が属する連結成分のサイズである 1 を出力します。
- 4 番目のクエリでは、S に 7 が追加され、S=\{3,7,10\} となります。
- 5 番目のクエリでは、3,7,10 の 3 頂点からなり、3 と 7、7 と 10 の間に辺のあるグラフを考え、3 が属する連結成分のサイズである 3 を出力します。
- 6 番目のクエリでは、S から 10 が削除され、S=\{3,7\} となります。
- 7 番目のクエリでは、3,7 の 2 頂点からなり、3 と 7 の間に辺のあるグラフを考え、3 が属する連結成分のサイズである 2 を出力します。
入力例 2
11 1000000000000000000 1 1 1 100 1 10000 1 1000000 1 100000000 1 10000000000 1 1000000000000 1 100000000000000 1 10000000000000000 1 1000000000000000000 2 1
出力例 2
10
入力例 3
8 0 1 1 1 2 2 1 1 1 1 2 1 1 1 2 2 1
出力例 3
1 1
Score : 525 points
Problem Statement
You are given an integer K. For a set S that is initially empty, process Q queries of the following two types in order:
1 x: An integer x is given. If x is in S, remove x from S. Otherwise, add x to S.2 x: An integer x that is in S is given. Consider a graph where the vertices are the numbers in S, and there is an edge between two numbers if and only if the absolute difference between them is at most K. Print the count of vertices in the connected component that contains x.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq K \leq 10^{18}
- For each query, 1 \leq x \leq 10^{18}.
- For each query of the second type, the given x is in S at that point.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q K
\mathrm{query}_1
\vdots
\mathrm{query}_Q
Each query is given in the following format:
1 x
2 x
Output
Process the queries.
Sample Input 1
7 5 1 3 1 10 2 3 1 7 2 3 1 10 2 3
Sample Output 1
1 3 2
The query processing proceeds as follows:
- In the first query, 3 is added to S, resulting in S=\{3\}.
- In the second query, 10 is added to S, resulting in S=\{3,10\}.
- In the third query, consider a graph with vertices 3 and 10 and no edges. The connected component containing 3 has a size of 1, so print 1.
- In the fourth query, 7 is added to S, resulting in S=\{3,7,10\}.
- In the fifth query, consider a graph with vertices 3, 7, and 10, with edges between 3 and 7, and 7 and 10. The connected component containing 3 has a size of 3, so print 3.
- In the sixth query, 10 is removed from S, resulting in S=\{3,7\}.
- In the seventh query, consider a graph with vertices 3 and 7, with an edge between them. The connected component containing 3 has a size of 2, so print 2.
Sample Input 2
11 1000000000000000000 1 1 1 100 1 10000 1 1000000 1 100000000 1 10000000000 1 1000000000000 1 100000000000000 1 10000000000000000 1 1000000000000000000 2 1
Sample Output 2
10
Sample Input 3
8 0 1 1 1 2 2 1 1 1 1 2 1 1 1 2 2 1
Sample Output 3
1 1