Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
文字列 S が与えられます。ここで、S の 1 文字目は英大文字、2 文字目以降は英小文字です。
S の 1 文字目と UPC をこの順に結合した文字列を出力してください。
制約
- S は長さ 1 以上 100 以下の文字列
- S の 1 文字目は英大文字
- S の 2 文字目以降は英小文字
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の 1 文字目と UPC をこの順に結合した文字列を出力せよ。
入力例 1
Kyoto
出力例 1
KUPC
Kyoto の 1 文字目は K であるため、K と UPC を結合した KUPC を出力します。
入力例 2
Tohoku
出力例 2
TUPC
Score : 100 points
Problem Statement
You are given a string S. Here, the first character of S is an uppercase English letter, and the second and subsequent characters are lowercase English letters.
Print the string formed by concatenating the first character of S and UPC in this order.
Constraints
- S is a string of length between 1 and 100, inclusive.
- The first character of S is an uppercase English letter.
- The second and subsequent characters of S are lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the string formed by concatenating the first character of S and UPC in this order.
Sample Input 1
Kyoto
Sample Output 1
KUPC
The first character of Kyoto is K, so concatenate K and UPC, and print KUPC.
Sample Input 2
Tohoku
Sample Output 2
TUPC
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
数字からなる文字列 S が与えられます。
S から 2 以外の文字を削除し、残った文字を順序を保って結合した文字列を求めてください。
制約
- S は数字からなる長さ 1 以上 100 以下の文字列
- S は
2を 1 つ以上含む
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
20250222
出力例 1
22222
20250222 から 0, 5, 0 を削除し、残った文字を順序を保って結合することで文字列 22222 が得られます。
入力例 2
2
出力例 2
2
入力例 3
22222000111222222
出力例 3
22222222222
Score : 100 points
Problem Statement
You are given a string S consisting of digits.
Remove all characters from S except for 2, and then concatenate the remaining characters in their original order to form a new string.
Constraints
- S is a string consisting of digits with length between 1 and 100, inclusive.
- S contains at least one
2.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
20250222
Sample Output 1
22222
By removing 0, 5, and 0 from 20250222 and then concatenating the remaining characters in their original order, the string 22222 is obtained.
Sample Input 2
2
Sample Output 2
2
Sample Input 3
22222000111222222
Sample Output 3
22222222222
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。
座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。
高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。
制約
- -1000 \leq X,Y,Z \leq 1000
- X,Y,Z は相異なり、いずれも 0 でない
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
X Y Z
出力
高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1 と出力せよ。
入力例 1
10 -10 1
出力例 1
10
高橋君はまっすぐゴールに向かうことができます。
入力例 2
20 10 -10
出力例 2
40
ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。
入力例 3
100 1 1000
出力例 3
-1
Score : 200 points
Problem Statement
Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.
There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.
Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.
Constraints
- -1000 \leq X,Y,Z \leq 1000
- X, Y, and Z are distinct, and none of them is 0.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
X Y Z
Output
If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.
Sample Input 1
10 -10 1
Sample Output 1
10
Takahashi can go straight to the goal.
Sample Input 2
20 10 -10
Sample Output 2
40
The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.
Sample Input 3
100 1 1000
Sample Output 3
-1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数 N が与えられます。
以下の指示に従って N の近似値を出力してください。
- N が 10^3-1 以下ならば、N をそのまま出力する。
- N が 10^3 以上 10^4-1 以下ならば、N の一の位を切り捨てて出力する。
- N が 10^4 以上 10^5-1 以下ならば、N の十の位以下を切り捨てて出力する。
- N が 10^5 以上 10^6-1 以下ならば、N の百の位以下を切り捨てて出力する。
- N が 10^6 以上 10^7-1 以下ならば、N の千の位以下を切り捨てて出力する。
- N が 10^7 以上 10^8-1 以下ならば、N の一万の位以下を切り捨てて出力する。
- N が 10^8 以上 10^9-1 以下ならば、N の十万の位以下を切り捨てて出力する。
制約
- N は 0 以上 10^9-1 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
20230603
出力例 1
20200000
20230603 は 10^7 以上 10^8-1 以下です。
したがって、一万以下の位を切り捨てて 20200000 を出力します。
入力例 2
0
出力例 2
0
入力例 3
304
出力例 3
304
入力例 4
500600
出力例 4
500000
Score : 200 points
Problem Statement
You are given an integer N.
Print an approximation of N according to the following instructions.
- If N is less than or equal to 10^3-1, print N as it is.
- If N is between 10^3 and 10^4-1, inclusive, truncate the ones digit of N and print the result.
- If N is between 10^4 and 10^5-1, inclusive, truncate the tens digit and all digits below it of N and print the result.
- If N is between 10^5 and 10^6-1, inclusive, truncate the hundreds digit and all digits below it of N and print the result.
- If N is between 10^6 and 10^7-1, inclusive, truncate the thousands digit and all digits below it of N and print the result.
- If N is between 10^7 and 10^8-1, inclusive, truncate the ten-thousands digit and all digits below it of N and print the result.
- If N is between 10^8 and 10^9-1, inclusive, truncate the hundred-thousands digit and all digits below it of N and print the result.
Constraints
- N is an integer between 0 and 10^9-1, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
20230603
Sample Output 1
20200000
20230603 is between 10^7 and 10^8-1 (inclusive).
Therefore, truncate the ten-thousands digit and all digits below it, and print 20200000.
Sample Input 2
0
Sample Output 2
0
Sample Input 3
304
Sample Output 3
304
Sample Input 4
500600
Sample Output 4
500000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋くんは数直線上に N 個のプレゼントを置きました。そのうち i 個目のプレゼントは座標 A_i に置かれました。
あなたは数直線上の長さ M の半開区間 [x,x+M) を選び、そこに含まれるプレゼントを全て獲得します。
より詳しくは、以下の手順でプレゼントを獲得します。
- まず、実数 x をひとつ選択する。
- その後、プレゼントのうち置かれている座標が x \le A_i < x+M を満たすものを全て獲得する。
最大でいくつのプレゼントを獲得することができますか?
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 1 \le M \le 10^9
- 0 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
8 6 2 3 5 7 11 13 17 19
出力例 1
4
例えば、半開区間 [1.5,7.5) を指定します。
このとき、座標 2,3,5,7 にある 4 つのプレゼントを全て獲得することができ、これが獲得可能な最大の個数です。
入力例 2
10 1 3 1 4 1 5 9 2 6 5 3
出力例 2
2
同一の座標に複数のプレゼントが置いてあることもあります。
入力例 3
10 998244353 100000007 0 1755647 998244353 495 1000000000 1755648 503 1755649 998244853
出力例 3
7
Score : 300 points
Problem Statement
Takahashi has placed N gifts on a number line. The i-th gift is placed at coordinate A_i.
You will choose a half-open interval [x,x+M) of length M on the number line and acquire all the gifts included in it.
More specifically, you acquire gifts according to the following procedure.
- First, choose one real number x.
- Then, acquire all the gifts whose coordinates satisfy x \le A_i < x+M.
What is the maximum number of gifts you can acquire?
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- 1 \le M \le 10^9
- 0 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
8 6 2 3 5 7 11 13 17 19
Sample Output 1
4
For example, specify the half-open interval [1.5,7.5).
In this case, you can acquire the four gifts at coordinates 2,3,5,7, the maximum number of gifts that can be acquired.
Sample Input 2
10 1 3 1 4 1 5 9 2 6 5 3
Sample Output 2
2
There may be multiple gifts at the same coordinate.
Sample Input 3
10 998244353 100000007 0 1755647 998244353 495 1000000000 1755648 503 1755649 998244853
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の数列 A=(a_1,\ldots,a_N) があります。また、整数 K が与えられます。
あなたは次の操作を 0 回以上何度でも行えます。
- 1 \leq i \leq N-K を満たす整数 i を選び、a_i と a_{i+K} の値を入れ替える。
A を昇順に並べ替えることが出来るかどうかを判定してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N-1
- 1 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K a_1 \ldots a_N
出力
A を昇順に並び替えることが出来るならば Yes と、出来ないならば No と出力せよ。
入力例 1
5 2 3 4 1 3 4
出力例 1
Yes
次のように操作をすることで A を昇順に並び替えることが出来ます。
- i=1 とし、a_1 と a_3 の値を入れ替える。数列は (1,4,3,3,4) となる。
- i=2 とし、a_2 と a_4 の値を入れ替える。数列は (1,3,3,4,4) となる。
入力例 2
5 3 3 4 1 3 4
出力例 2
No
入力例 3
7 5 1 2 3 4 5 5 10
出力例 3
Yes
操作を行う必要が無い場合もあります。
Score : 300 points
Problem Statement
We have a sequence of length N: A=(a_1,\ldots,a_N). Additionally, you are given an integer K.
You can perform the following operation zero or more times.
- Choose an integer i such that 1 \leq i \leq N-K, then swap the values of a_i and a_{i+K}.
Determine whether it is possible to sort A in ascending order.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N-1
- 1 \leq a_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K a_1 \ldots a_N
Output
If it is possible to sort A in ascending order, print Yes; otherwise, print No.
Sample Input 1
5 2 3 4 1 3 4
Sample Output 1
Yes
The following sequence of operations sorts A in ascending order.
- Choose i=1 to swap the values of a_1 and a_3. A is now (1,4,3,3,4).
- Choose i=2 to swap the values of a_2 and a_4. A is now (1,3,3,4,4).
Sample Input 2
5 3 3 4 1 3 4
Sample Output 2
No
Sample Input 3
7 5 1 2 3 4 5 5 10
Sample Output 3
Yes
No operations may be needed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
H 行 W 列のマス目があり、いくつか(0 個のこともある)のマスには磁石が置かれています。
マス目の状態は H 個の 長さ W の文字列 S_1,S_2,\ldots,S_H で表され、
S_i の j 文字目が # のとき上から i 行目かつ左から j 列目のマスには磁石が置かれていることを、
. のとき何も置かれていないことを表しています。
高橋君は鉄の鎧を着ており、あるマスにいるとき次のように移動することができます。
- 現在いるマスの上下左右に隣り合うマスのいずれかに磁石が置かれているとき、どこへも移動することができない。
- そうでないとき、上下左右に隣り合うマスのいずれかを選んでそのマスに移動することができる。
ただし、マス目の外に移動することはできない。
磁石が置かれていない各マスについて、そのマスの自由度を、「最初高橋くんがそのマスにいるとき、そこから移動を繰り返して到達できるマスの個数」として定義します。 マス目のうち磁石が置かれていないマスの中における、マスの自由度の最大値を求めてください。
ただし、自由度の定義において、「移動を繰り返して到達できるマス」とは、最初にいるマスからそのマスまで移動を繰り返して到達する方法(1 回も移動しないものも含む)が 1 つ以上存在するようなマスのことであり、 最初のマスから始めてすべてのそのようなマスを巡るような移動方法が存在する必要はありません。特に(磁石の置かれていない)各マス自身は、そのマスから「移動を繰り返して到達できるマス」につねに含まれることに注意してください。
制約
- 1\leq H,W\leq 1000
- H,W は整数
- S_i は
.と#のみからなる長さ W の文字列 - 磁石の置かれていないマスが少なくとも 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
マス目のうち磁石が置かれていないマスの中における、マスの自由度の最大値を出力せよ。
入力例 1
3 5 .#... ..... .#..#
出力例 1
9
上から i 行目かつ左から j 列目のマスを (i,j) で表します。 高橋君が最初に (2,3) にいるとき、高橋君の移動の例としては次のようなものなどが考えられます。
- (2,3)\to (2,4)\to (1,4)\to (1,5)\to (2,5)
- (2,3)\to (2,4)\to (3,4)
- (2,3)\to (2,2)
- (2,3)\to (1,3)
- (2,3)\to (3,3)
よって、途中で到達しているマスも含めて高橋君は (2,3) から少なくとも 9 個のマスに到達することができます。 一方、これら以外のマスには到達することができないため、(2,3) の自由度は 9 となります。
これは磁石が置かれていない各マスの自由度のうち最大であるため、9 を出力します。
入力例 2
3 3 ..# #.. ..#
出力例 2
1
磁石が置かれていないどのマスについても、上下左右に隣り合うマスのいずれかに磁石が置かれています。
よって、磁石が置かれていないどのマスからも移動することはできず、マスの自由度は 1 となります。
そのため、1 を出力します。
Score: 425 points
Problem Statement
There is a grid of H rows and W columns. Some cells (possibly zero) contain magnets.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H of length W. If the j-th character of S_i is #, it indicates that there is a magnet in the cell at the i-th row from the top and j-th column from the left; if it is ., it indicates that the cell is empty.
Takahashi, wearing an iron armor, can move in the grid as follows:
- If any of the cells vertically or horizontally adjacent to the current cell contains a magnet, he cannot move at all.
- Otherwise, he can move to any one of the vertically or horizontally adjacent cells.
However, he cannot exit the grid.
For each cell without a magnet, define its degree of freedom as the number of cells he can reach by repeatedly moving from that cell. Find the maximum degree of freedom among all cells without magnets in the grid.
Here, in the definition of degree of freedom, "cells he can reach by repeatedly moving" mean cells that can be reached from the initial cell by some sequence of moves (possibly zero moves). It is not necessary that there is a sequence of moves that visits all such reachable cells starting from the initial cell. Specifically, each cell itself (without a magnet) is always included in the cells reachable from that cell.
Constraints
- 1 \leq H, W \leq 1000
- H and W are integers.
- S_i is a string of length W consisting of
.and#. - There is at least one cell without a magnet.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print the maximum degree of freedom among all cells without magnets.
Sample Input 1
3 5 .#... ..... .#..#
Sample Output 1
9
Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. If Takahashi starts at (2,3), possible movements include:
- (2,3) \to (2,4) \to (1,4) \to (1,5) \to (2,5)
- (2,3) \to (2,4) \to (3,4)
- (2,3) \to (2,2)
- (2,3) \to (1,3)
- (2,3) \to (3,3)
Thus, including the cells he passes through, he can reach at least nine cells from (2,3).
Actually, no other cells can be reached, so the degree of freedom for (2,3) is 9.
This is the maximum degree of freedom among all cells without magnets, so print 9.
Sample Input 2
3 3 ..# #.. ..#
Sample Output 2
1
For any cell without a magnet, there is a magnet in at least one of the adjacent cells.
Thus, he cannot move from any of these cells, so their degrees of freedom are 1.
Therefore, print 1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点 M 辺の単純連結無向グラフが与えられます。
辺 i は頂点 A_i と頂点 B_i を結ぶ長さ C_i の辺です。
以下の条件を満たすようにいくつかの辺を削除します。削除する辺の数の最大値を求めてください。
- 辺を削除した後のグラフも連結である。
- 全ての頂点対 (s,t) について、頂点 s と頂点 t の間の距離が削除前と削除後で変化しない。
注釈
単純連結無向グラフとは、単純かつ連結で辺に向きの無いグラフのことをいいます。
グラフが単純であるとは、グラフが自己ループや多重辺を含まないことをいいます。
グラフが連結であるとは、グラフ上の任意の 2 頂点 s, t について s から t へ辺をたどって行けることをいいます。
頂点 s と頂点 t の間の距離とは、頂点 s と頂点 t の間の最短路の長さのことをいいます。
制約
- 2 \leq N \leq 300
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i \lt B_i \leq N
- 1 \leq C_i \leq 10^9
- i \neq j ならば (A_i, B_i) \neq (A_j, B_j) である。
- 与えられるグラフは連結である。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
答えを出力せよ。
入力例 1
3 3 1 2 2 2 3 3 1 3 6
出力例 1
1
辺を削除する前の全ての頂点対の距離は次の通りです。
- 頂点 1 と頂点 2 の距離は 2
- 頂点 1 と頂点 3 の距離は 5
- 頂点 2 と頂点 3 の距離は 3
辺 3 を削除しても全ての頂点間の距離は変化しません。また、問題文の条件を満たすように 2 本以上の辺を削除することはできないので、答えは 1 本になります。
入力例 2
5 4 1 3 3 2 3 9 3 5 3 4 5 3
出力例 2
0
どの辺も削除することができません。
入力例 3
5 10 1 2 71 1 3 9 1 4 82 1 5 64 2 3 22 2 4 99 2 5 1 3 4 24 3 5 18 4 5 10
出力例 3
5
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges.
Edge i connects Vertex A_i and Vertex B_i, and has a length of C_i.
Let us delete some number of edges while satisfying the conditions below. Find the maximum number of edges that can be deleted.
- The graph is still connected after the deletion.
- For every pair of vertices (s, t), the distance between s and t remains the same before and after the deletion.
Notes
A simple connected undirected graph is a graph that is simple and connected and has undirected edges.
A graph is simple when it has no self-loops and no multi-edges.
A graph is connected when, for every pair of two vertices s and t, t is reachable from s by traversing edges.
The distance between Vertex s and Vertex t is the length of a shortest path between s and t.
Constraints
- 2 \leq N \leq 300
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i \lt B_i \leq N
- 1 \leq C_i \leq 10^9
- (A_i, B_i) \neq (A_j, B_j) if i \neq j.
- The given graph is connected.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the answer.
Sample Input 1
3 3 1 2 2 2 3 3 1 3 6
Sample Output 1
1
The distance between each pair of vertices before the deletion is as follows.
- The distance between Vertex 1 and Vertex 2 is 2.
- The distance between Vertex 1 and Vertex 3 is 5.
- The distance between Vertex 2 and Vertex 3 is 3.
Deleting Edge 3 does not affect the distance between any pair of vertices. It is impossible to delete two or more edges while satisfying the condition, so the answer is 1.
Sample Input 2
5 4 1 3 3 2 3 9 3 5 3 4 5 3
Sample Output 2
0
No edge can be deleted.
Sample Input 3
5 10 1 2 71 1 3 9 1 4 82 1 5 64 2 3 22 2 4 99 2 5 1 3 4 24 3 5 18 4 5 10
Sample Output 3
5
Time Limit: 2.5 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
高橋君はAtCoderのコンテストに N 回参加しようとしています。
i 回目 (1 \leq i \leq N) のコンテストでは、レーティングが L_i 以上 R_i 以下である場合、レーティングが 1 増加します。
以下の形式で与えられる Q 個のクエリに答えてください。
- 整数 X が与えられる。高橋君の最初のレーティングが X であった場合、N 回のコンテストを終えた後のレーティングを求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
- 1 \leq Q \leq 3 \times 10^5
- 各クエリについて 1 \leq X \leq 5 \times 10^5
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ただし、\text{query}_i は i 個目のクエリを表し、以下の形式である。
X
出力
Q 行出力せよ。i 行目には、i 個目のクエリに対する答えを出力せよ。
入力例 1
5 1 5 1 3 3 6 2 4 4 7 3 3 2 5
出力例 1
6 6 8
1 個目のクエリでは以下のようにコンテストごとにレーティングが変化します。
- 1 回目のコンテストではレーティングが 1 以上 5 以下のため、レーティングが 1 増加し 4 になります。
- 2 回目のコンテストではレーティングが 1 以上 3 以下でないため、レーティングが増加せず 4 のままです。
- 3 回目のコンテストではレーティングが 3 以上 6 以下のため、レーティングが 1 増加し 5 になります。
- 4 回目のコンテストではレーティングが 2 以上 4 以下でないため、レーティングが増加せず 5 のままです。
- 5 回目のコンテストではレーティングが 4 以上 7 以下のため、レーティングが 1 増加し 6 になります。
2 個目のクエリでは 1,2,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 6 になります。
3 個目のクエリでは 1,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 8 になります。
入力例 2
10 1 1999 1 1999 1200 2399 1 1999 1 1999 1 1999 2000 500000 1 1999 1 1999 1600 2799 7 1 1995 2000 2399 500000 2799 1000
出力例 2
8 2002 2003 2402 500001 2800 1007
入力例 3
15 260522 414575 436426 479445 148772 190081 190629 433447 47202 203497 394325 407775 304784 463982 302156 468417 131932 235902 78537 395728 223857 330739 286918 329211 39679 238506 63340 186568 160016 361868 10 287940 296263 224593 101449 336991 390310 323355 177068 11431 8580
出力例 3
287946 296269 224599 101453 336997 390315 323363 177075 11431 8580
Score : 525 points
Problem Statement
Takahashi plans to participate in N AtCoder contests.
In the i-th contest (1 \leq i \leq N), if his rating is between L_i and R_i (inclusive), his rating increases by 1.
You are given Q queries in the following format:
- An integer X is given. Assuming that Takahashi's initial rating is X, determine his rating after participating in all N contests.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
- 1 \leq Q \leq 3 \times 10^5
- For each query, 1 \leq X \leq 5 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i is the i-th query in the form:
X
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Sample Input 1
5 1 5 1 3 3 6 2 4 4 7 3 3 2 5
Sample Output 1
6 6 8
For the 1st query, the rating changes as follows:
- In the 1st contest, the rating is between 1 and 5, so it increases by 1, becoming 4.
- In the 2nd contest, the rating is not between 1 and 3, so it remains 4.
- In the 3rd contest, the rating is between 3 and 6, so it increases by 1, becoming 5.
- In the 4th contest, the rating is not between 2 and 4, so it remains 5.
- In the 5th contest, the rating is between 4 and 7, so it increases by 1, becoming 6.
For the 2nd query, the rating increases in the 1st, 2nd, 3rd, and 5th contests, ending at 6.
For the 3rd query, the rating increases in the 1st, 3rd, and 5th contests, ending at 8.
Sample Input 2
10 1 1999 1 1999 1200 2399 1 1999 1 1999 1 1999 2000 500000 1 1999 1 1999 1600 2799 7 1 1995 2000 2399 500000 2799 1000
Sample Output 2
8 2002 2003 2402 500001 2800 1007
Sample Input 3
15 260522 414575 436426 479445 148772 190081 190629 433447 47202 203497 394325 407775 304784 463982 302156 468417 131932 235902 78537 395728 223857 330739 286918 329211 39679 238506 63340 186568 160016 361868 10 287940 296263 224593 101449 336991 390310 323355 177068 11431 8580
Sample Output 3
287946 296269 224599 101453 336997 390315 323363 177075 11431 8580