Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は木工工房で働いています。工房には N 本の木の棒があり、i 番目の棒の長さは A_i センチメートルです。
高橋君はこれらの棒を使って柵を作ることになりました。柵を美しく仕上げるためには、すべての棒の長さを同じ正の整数の長さに揃える必要があります。
高橋君は以下の操作を何度でも繰り返し行うことができます。
- 棒を 1 本選び、その棒の長さをちょうど 1 センチメートル短くする。
同じ棒を複数回選んでも構いません。ただし、棒を伸ばすことはできず、どの棒の長さも常に 1 センチメートル以上でなければなりません。
高橋君は目標の長さを自由に選ぶことができます。具体的には、正の整数 T を 1 つ決め、操作を繰り返すことで、すべての棒の長さを T センチメートルにします。棒を伸ばすことはできないため、T はすべての棒の現在の長さ以下、すなわち 1 \leq T \leq \min(A_1, A_2, \ldots, A_N) を満たす必要があります。
T を最適に選んだとき、すべての棒の長さを T に揃えるために必要な操作回数の総和の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
N A_1 A_2 \ldots A_N
- 1 行目には、棒の本数を表す整数 N が与えられる。
- 2 行目には、各棒の長さを表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
すべての棒の長さを等しい正の整数にするために必要な最小の操作回数を 1 行で出力せよ。
入力例 1
3 5 3 4
出力例 1
3
入力例 2
5 10 10 10 10 10
出力例 2
0
入力例 3
7 100 45 72 89 50 63 45
出力例 3
149
Score : 233 pts
Problem Statement
Takahashi works at a woodworking workshop. The workshop has N wooden sticks, and the length of the i-th stick is A_i centimeters.
Takahashi needs to use these sticks to build a fence. To make the fence look beautiful, all sticks must be adjusted to the same positive integer length.
Takahashi can perform the following operation any number of times:
- Choose one stick and shorten its length by exactly 1 centimeter.
The same stick may be chosen multiple times. However, sticks cannot be lengthened, and the length of every stick must remain at least 1 centimeter at all times.
Takahashi is free to choose the target length. Specifically, he decides on a single positive integer T and repeats the operation until all sticks have length T centimeters. Since sticks cannot be lengthened, T must be at most the current length of every stick, i.e., 1 \leq T \leq \min(A_1, A_2, \ldots, A_N).
Find the minimum total number of operations needed to make all sticks have length T, when T is chosen optimally.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All inputs are integers
Input
N A_1 A_2 \ldots A_N
- The first line contains an integer N, representing the number of sticks.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the length of each stick.
Output
Print in one line the minimum number of operations required to make all sticks equal to the same positive integer length.
Sample Input 1
3 5 3 4
Sample Output 1
3
Sample Input 2
5 10 10 10 10 10
Sample Output 2
0
Sample Input 3
7 100 45 72 89 50 63 45
Sample Output 3
149
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は遊園地のチケット窓口で働いています。今日は N 人のお客さんが順番に並んでおり、i 番目のお客さんの予算は S_i 円です。
この遊園地には M 種類のチケットがあり、j 番目のチケットの価格は G_j 円です。チケットの価格はすべて異なります。各種類のチケットは、今日 1 日で最大 K 枚までしか販売できません。
高橋君は N 人のお客さんを並んでいる順(1 番目のお客さんから N 番目のお客さんまで)に、以下のルールで 1 人ずつ処理していきます。1 人のお客さんに販売するチケットは最大 1 枚であり、あるお客さんへの処理が完了してから次のお客さんの処理に移ります。
- そのお客さんの予算以下の価格のチケット(すなわち G_j \leq S_i を満たすチケット j)のうち、価格が最も高いものを選びます。チケットの価格はすべて異なるため、条件を満たすチケットがあれば選ばれるものは一意に定まります。予算以下の価格のチケットが存在しない場合、そのお客さんにはチケットを販売せず、次のお客さんの処理に移ります。
- 手順 1 で選んだチケットについて、本日の販売済み枚数が K 枚未満であれば、そのチケットを 1 枚販売します。既に K 枚販売済みである場合、そのお客さんにはチケットを販売せず、次のお客さんの処理に移ります。手順 1 で選んだチケットが販売できなかった場合でも、別の種類のチケットを代わりに販売することはありません。
高橋君がチケットを販売できるお客さんの人数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq K \leq N
- 1 \leq S_i \leq 10^{18} (1 \leq i \leq N)
- S_i の値は重複することがある。
- 1 \leq G_j \leq 10^{18} (1 \leq j \leq M)
- G_j はすべて異なる。
- 入力はすべて整数である。
入力
N M K S_1 S_2 \ldots S_N G_1 G_2 \ldots G_M
- 1 行目には、お客さんの人数を表す N、チケットの種類数を表す M、各チケットの 1 日あたりの最大販売枚数を表す K が、空白区切りで与えられる。
- 2 行目には、各お客さんの予算を表す S_1, S_2, \ldots, S_N が、空白区切りで与えられる。
- 3 行目には、各チケットの価格を表す G_1, G_2, \ldots, G_M が、空白区切りで与えられる。
出力
高橋君がチケットを販売できるお客さんの人数を 1 行で出力せよ。
入力例 1
5 3 2 10 5 8 12 6 3 7 11
出力例 1
5
入力例 2
4 2 1 5 5 3 10 5 8
出力例 2
2
入力例 3
10 5 2 100 50 30 80 60 40 90 20 70 55 25 45 65 85 100
出力例 3
8
入力例 4
20 6 3 500 200 350 700 150 600 400 250 800 100 450 550 300 650 50 750 900 200 350 600 120 280 460 590 710 850
出力例 4
14
入力例 5
1 1 1 5 10
出力例 5
0
Score : 333 pts
Problem Statement
Takahashi works at the ticket window of an amusement park. Today, N customers are lined up in order, and the i-th customer has a budget of S_i yen.
The amusement park offers M types of tickets, and the j-th ticket costs G_j yen. All ticket prices are distinct. Each type of ticket can be sold at most K times in a single day.
Takahashi processes the N customers one by one in the order they are lined up (from the 1-st customer to the N-th customer), following the rules below. At most one ticket is sold to each customer, and the next customer is processed only after the current customer's processing is complete.
- Among the tickets whose price is within the customer's budget (i.e., tickets j satisfying G_j \leq S_i), select the one with the highest price. Since all ticket prices are distinct, if there exists a ticket satisfying the condition, the selection is unique. If no ticket has a price within the customer's budget, no ticket is sold to that customer, and processing moves to the next customer.
- For the ticket selected in step 1, if fewer than K copies have been sold today, sell one copy of that ticket. If K copies have already been sold, no ticket is sold to that customer, and processing moves to the next customer. Even if the ticket selected in step 1 cannot be sold, no other type of ticket will be sold instead.
Determine the number of customers to whom Takahashi can sell a ticket.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq K \leq N
- 1 \leq S_i \leq 10^{18} (1 \leq i \leq N)
- The values of S_i may contain duplicates.
- 1 \leq G_j \leq 10^{18} (1 \leq j \leq M)
- All G_j are distinct.
- All inputs are integers.
Input
N M K S_1 S_2 \ldots S_N G_1 G_2 \ldots G_M
- The first line contains N (the number of customers), M (the number of ticket types), and K (the maximum number of each ticket that can be sold per day), separated by spaces.
- The second line contains S_1, S_2, \ldots, S_N (the budget of each customer), separated by spaces.
- The third line contains G_1, G_2, \ldots, G_M (the price of each ticket), separated by spaces.
Output
Print the number of customers to whom Takahashi can sell a ticket, on a single line.
Sample Input 1
5 3 2 10 5 8 12 6 3 7 11
Sample Output 1
5
Sample Input 2
4 2 1 5 5 3 10 5 8
Sample Output 2
2
Sample Input 3
10 5 2 100 50 30 80 60 40 90 20 70 55 25 45 65 85 100
Sample Output 3
8
Sample Input 4
20 6 3 500 200 350 700 150 600 400 250 800 100 450 550 300 650 50 750 900 200 350 600 120 280 460 590 710 850
Sample Output 4
14
Sample Input 5
1 1 1 5 10
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は農業用水の管理者です。管轄する地域には N 個の貯水池があり、貯水池は 1 から N までの番号で管理されています。現在、各貯水池 i (1 \leq i \leq N) には P_i リットルの水が蓄えられています。
地域には M 本の水路があり、j 番目の水路は貯水池 U_j と貯水池 V_j を双方向に結んでいます。なお、すべての貯水池が水路を通じて互いにつながっているとは限りません。
高橋君は、すべての貯水池の水量の最大値と最小値の差をできるだけ小さくしたいと考えています。そのために、以下の操作を 0 回以上、好きな回数だけ行うことができます。同じ水路を複数回使うこともできます。
操作: 水路で直接つながっている貯水池の組 (u, v) を 1 つ選び、一方からもう一方へちょうど 1 リットルの水を移動させる。
1 回の操作では水路で直接つながっている貯水池の間でしか水を移動できませんが、操作を繰り返すことで、水路で間接的につながっている貯水池へ水を運ぶこともできます。一方、水路によってつながっていない貯水池の間では、どのように操作を行っても水を移動させることはできません。
ただし、設備の保全上、どの操作の直後においても、すべての貯水池の水量は 1 リットル以上でなければなりません。すなわち、ある操作で貯水池から水を 1 リットル送った結果、その貯水池の水量が 0 リットルになるような操作は行えません。
なお、各貯水池の水量は常に整数リットルです(初期水量が整数であり、操作は 1 リットル単位で行うため)。
操作を最適に行ったとき、すべての貯水池の水量の最大値から最小値を引いた差として達成できる最小の値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq P_i \leq 10^9
- 1 \leq U_j, V_j \leq N
- U_j \neq V_j(自己ループはない)
- 同じ貯水池の組を結ぶ水路は高々 1 本である(多重辺はない)
- 入力はすべて整数である
入力
N M P_1 P_2 \ldots P_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- 1 行目には、貯水池の数を表す整数 N と、水路の数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各貯水池の初期水量を表す整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
- 続く M 行では、各水路が結ぶ 2 つの貯水池の番号が与えられる。
- 2 + j 行目 (1 \leq j \leq M) では、j 番目の水路が結ぶ貯水池の番号 U_j と V_j がスペース区切りで与えられる。
出力
操作を最適に行ったとき、すべての貯水池の水量の最大値と最小値の差として達成できる最小の値を 1 行で出力せよ。
入力例 1
3 2 1 5 2 1 2 2 3
出力例 1
1
入力例 2
4 0 3 1 4 1
出力例 2
3
入力例 3
10 8 10 1 7 4 3 20 2 8 8 15 1 2 2 3 3 4 1 4 5 6 6 7 5 7 8 9
出力例 3
10
入力例 4
20 24 100 1 50 75 30 200 10 90 60 5 130 40 1000000000 999999999 2 3 4 500 1 1000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 1 6 2 7 3 8 4 9 13 14 14 15 15 16 16 17 17 18 13 16 14 17 15 18 19 20
出力例 4
333333353
入力例 5
1 0 1000000000
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is the manager of agricultural water. The region he manages has N reservoirs, numbered from 1 to N. Currently, each reservoir i (1 \leq i \leq N) stores P_i liters of water.
The region has M waterways, and the j-th waterway bidirectionally connects reservoir U_j and reservoir V_j. Note that it is not necessarily the case that all reservoirs are connected to each other through waterways.
Takahashi wants to minimize the difference between the maximum and minimum water amounts among all reservoirs. To achieve this, he can perform the following operation zero or more times, as many times as he likes. The same waterway can be used multiple times.
Operation: Choose a pair of reservoirs (u, v) that are directly connected by a waterway, and transfer exactly 1 liter of water from one to the other.
In a single operation, water can only be moved between reservoirs that are directly connected by a waterway. However, by repeating operations, water can also be transported to reservoirs that are indirectly connected through waterways. On the other hand, no matter what operations are performed, water cannot be moved between reservoirs that are not connected by any waterways.
However, for equipment maintenance purposes, immediately after every operation, the water amount in every reservoir must be at least 1 liter. That is, an operation that would result in a reservoir having 0 liters of water after sending 1 liter from it is not allowed.
Note that the water amount in each reservoir is always an integer number of liters (since the initial water amounts are integers and operations are performed in units of 1 liter).
When operations are performed optimally, find the minimum achievable value of the difference between the maximum and minimum water amounts among all reservoirs.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq P_i \leq 10^9
- 1 \leq U_j, V_j \leq N
- U_j \neq V_j (no self-loops)
- There is at most one waterway connecting the same pair of reservoirs (no multi-edges)
- All input values are integers
Input
N M P_1 P_2 \ldots P_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- The first line contains an integer N representing the number of reservoirs and an integer M representing the number of waterways, separated by a space.
- The second line contains integers P_1, P_2, \ldots, P_N representing the initial water amounts in each reservoir, separated by spaces.
- The following M lines give the numbers of the two reservoirs connected by each waterway.
- The (2 + j)-th line (1 \leq j \leq M) contains the reservoir numbers U_j and V_j connected by the j-th waterway, separated by a space.
Output
Print in one line the minimum achievable value of the difference between the maximum and minimum water amounts among all reservoirs when operations are performed optimally.
Sample Input 1
3 2 1 5 2 1 2 2 3
Sample Output 1
1
Sample Input 2
4 0 3 1 4 1
Sample Output 2
3
Sample Input 3
10 8 10 1 7 4 3 20 2 8 8 15 1 2 2 3 3 4 1 4 5 6 6 7 5 7 8 9
Sample Output 3
10
Sample Input 4
20 24 100 1 50 75 30 200 10 90 60 5 130 40 1000000000 999999999 2 3 4 500 1 1000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 1 6 2 7 3 8 4 9 13 14 14 15 15 16 16 17 17 18 13 16 14 17 15 18 19 20
Sample Output 4
333333353
Sample Input 5
1 0 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、H 行 W 列のグリッド状に石のブロックが並んだ古代遺跡の調査を行っています。各マスには石のブロックが置かれており、上から i 行目、左から j 列目のブロックの 耐久値 は C_{i,j} です。
グリッドの外側の領域には常に風雨が吹き荒れています。ここで「グリッドの外側」とは、1 行目〜H 行目かつ 1 列目〜W 列目の範囲の外にある領域を指します。長い年月が経ち、石のブロックは風雨に触れている面から徐々に侵食されていきます。
石ブロックが「風雨にさらされている」とは、そのブロックの上下左右 4 方向の隣接マスのうち少なくとも 1 つが、グリッドの外側であるか、または空きマス(石ブロックが崩壊して何もなくなったマス)であることを意味します。初期状態では空きマスは存在しません。特に、グリッドの端(1 行目、H 行目、1 列目、W 列目)にあるブロックは、グリッドの外側に隣接しているため、初期状態から風雨にさらされています。
侵食は ステップ を単位として進行します。ステップには 1 から順に番号が付けられています。各ステップでは、以下の処理 1, 処理 2 がこの順に 1 回ずつ 行われます。
- 耐久値の減少: そのステップの開始時点で風雨にさらされているすべての石ブロックの耐久値が、同時に 1 だけ減少する。風雨にさらされていない石ブロックの耐久値は変化しない。
- 崩壊判定: 処理 1 の結果、耐久値が 0 になった石ブロックはすべて 同時に 崩壊し、そのマスは空きマスとなる。
同一ステップ内で連鎖的な崩壊は起こりません。すなわち、処理 2 で崩壊が起きた結果、新たに風雨にさらされるようになったブロックがあっても、そのブロックの耐久値がこのステップ中にさらに減少することはありません。新たに風雨にさらされるようになったブロックは、次のステップの処理 1 から耐久値の減少が始まります。
グリッドは有限であり、外周のブロックから順に侵食が内側へ進行するため、すべてのブロックは有限ステップで必ず崩壊します。したがって、どのマスについても崩壊するステップ番号は有限の正整数として定まります。
高橋君は、調査対象として指定された Q 個のマスについて、それぞれのマスにある石ブロックが崩壊するステップ番号を調べることにしました。
k 番目の調査対象は、上から R_k 行目、左から K_k 列目のマスです。Q 個の調査対象それぞれについて、そのマスの石ブロックが崩壊するステップ番号を求めてください。
制約
- 1 \leq H \leq 500
- 1 \leq W \leq 500
- 1 \leq C_{i,j} \leq 10^9
- 1 \leq Q \leq 10^5
- 1 \leq R_k \leq H
- 1 \leq K_k \leq W
- クエリで指定されるマスは重複することがある。
- 入力はすべて整数である。
入力
H W
C_{1,1} C_{1,2} \cdots C_{1,W}
C_{2,1} C_{2,2} \cdots C_{2,W}
\vdots
C_{H,1} C_{H,2} \cdots C_{H,W}
Q
R_1 K_1
R_2 K_2
\vdots
R_Q K_Q
- 1 行目には、遺跡の行数 H と列数 W がスペース区切りで与えられる。
- 続く H 行では、遺跡の各マスの石ブロックの耐久値が与えられる。このうち i 行目(1 \leq i \leq H)には、グリッドの i 行目の耐久値 C_{i,1}, C_{i,2}, \ldots, C_{i,W} がスペース区切りで与えられる。
- その次の行には、クエリの個数 Q が与えられる。
- 続く Q 行にわたり、各クエリが与えられる。k 番目のクエリ(1 \leq k \leq Q)では、問い合わせ対象の行番号 R_k と列番号 K_k がスペース区切りで与えられ、上から R_k 行目、左から K_k 列目のマスの石ブロックについて問う。
出力
Q 行出力せよ。k 行目(1 \leq k \leq Q)には、上から R_k 行目、左から K_k 列目の石ブロックが崩壊するステップの番号を整数で出力せよ。
入力例 1
3 3 1 2 1 3 5 2 1 1 4 5 1 1 2 2 3 3 2 1 1 3
出力例 1
1 6 4 3 1
入力例 2
1 5 4 1 3 2 5 6 1 1 1 2 1 3 1 4 1 5 1 3
出力例 2
4 1 3 2 5 3
入力例 3
6 7 3 8 2 6 4 7 1 5 12 9 10 3 11 4 6 14 20 18 13 9 2 4 15 22 17 16 8 5 7 10 6 12 9 14 3 2 5 8 4 6 1 9 12 1 1 1 7 2 3 3 4 4 3 4 5 5 2 6 6 3 1 6 7 2 6 5 5
出力例 3
3 1 11 34 36 29 15 1 6 9 15 15
入力例 4
12 12 5 18 7 21 9 30 12 25 6 17 11 4 14 40 33 28 19 45 37 31 22 26 35 8 10 29 55 48 42 60 51 46 39 44 20 13 16 34 58 70 65 72 69 63 57 50 27 15 23 41 62 80 95 90 88 76 61 53 32 18 9 36 59 84 100 120 110 92 74 56 38 24 20 43 67 89 115 130 125 101 83 64 47 28 12 31 54 78 96 112 105 87 71 49 30 19 8 27 45 66 79 91 86 73 58 42 25 14 6 22 39 52 68 77 70 55 43 33 16 10 11 24 30 37 46 59 48 40 29 21 13 7 3 15 9 26 12 34 18 23 5 20 8 2 20 1 1 1 12 12 1 12 12 6 6 7 7 5 8 8 5 3 4 10 10 2 2 11 11 4 9 9 4 6 1 1 6 12 9 7 12 5 5 5 5
出力例 4
5 4 3 2 408 448 240 271 97 59 54 20 124 146 9 30 5 28 230 230
入力例 5
1 1 1000000000 5 1 1 1 1 1 1 1 1 1 1
出力例 5
1000000000 1000000000 1000000000 1000000000 1000000000
Score : 400 pts
Problem Statement
Takahashi is investigating ancient ruins where stone blocks are arranged in a grid with H rows and W columns. Each cell contains a stone block, and the block at the i-th row from the top and j-th column from the left has a durability of C_{i,j}.
Wind and rain are constantly raging in the area outside the grid. Here, "outside the grid" refers to the region outside the range of rows 1 through H and columns 1 through W. Over many years, the stone blocks gradually erode starting from the surfaces that come into contact with the wind and rain.
A stone block is said to be "exposed to the wind and rain" if at least one of its four orthogonally adjacent cells (up, down, left, right) is outside the grid or is an empty cell (a cell where a stone block has collapsed and disappeared). Initially, there are no empty cells. In particular, the blocks on the boundary of the grid (1-st row, H-th row, 1-st column, and W-th column) are adjacent to the outside of the grid, so they are exposed to the wind and rain from the initial state.
Erosion progresses in units of steps, numbered starting from 1. In each step, the following Process 1 and Process 2 are performed once each in this order:
- Durability Reduction: The durability of every stone block that is exposed to the wind and rain at the start of this step decreases by 1 simultaneously. The durability of stone blocks not exposed to the wind and rain does not change.
- Collapse Check: All stone blocks whose durability becomes 0 as a result of Process 1 collapse simultaneously, and their cells become empty cells.
Cascading collapses do not occur within the same step. That is, even if some blocks become newly exposed to the wind and rain as a result of collapses in Process 2, the durability of those blocks will not decrease further during this step. Newly exposed blocks will begin to have their durability decreased starting from Process 1 of the next step.
Since the grid is finite and erosion progresses inward starting from the outer perimeter, all blocks will inevitably collapse within a finite number of steps. Therefore, the step number at which each cell's block collapses is uniquely determined as a finite positive integer.
Takahashi has decided to investigate the step number at which the stone block collapses for each of Q designated target cells.
The k-th target is the cell at the R_k-th row from the top and K_k-th column from the left. For each of the Q targets, find the step number at which the stone block at that cell collapses.
Constraints
- 1 \leq H \leq 500
- 1 \leq W \leq 500
- 1 \leq C_{i,j} \leq 10^9
- 1 \leq Q \leq 10^5
- 1 \leq R_k \leq H
- 1 \leq K_k \leq W
- The cells specified by the queries may contain duplicates.
- All input values are integers.
Input
H W
C_{1,1} C_{1,2} \cdots C_{1,W}
C_{2,1} C_{2,2} \cdots C_{2,W}
\vdots
C_{H,1} C_{H,2} \cdots C_{H,W}
Q
R_1 K_1
R_2 K_2
\vdots
R_Q K_Q
- The first line contains the number of rows H and the number of columns W of the ruins, separated by a space.
- The following H lines give the durabilities of the stone blocks at each cell of the ruins. The i-th line (1 \leq i \leq H) contains the durabilities C_{i,1}, C_{i,2}, \ldots, C_{i,W} of the i-th row of the grid, separated by spaces.
- The next line contains the number of queries, Q.
- The following Q lines describe the queries. The k-th query (1 \leq k \leq Q) contains the target row number R_k and column number K_k separated by a space, asking about the stone block at the R_k-th row from the top and K_k-th column from the left.
Output
Print Q lines. The k-th line (1 \leq k \leq Q) should contain an integer representing the step number at which the stone block at the R_k-th row from the top and K_k-th column from the left collapses.
Sample Input 1
3 3 1 2 1 3 5 2 1 1 4 5 1 1 2 2 3 3 2 1 1 3
Sample Output 1
1 6 4 3 1
Sample Input 2
1 5 4 1 3 2 5 6 1 1 1 2 1 3 1 4 1 5 1 3
Sample Output 2
4 1 3 2 5 3
Sample Input 3
6 7 3 8 2 6 4 7 1 5 12 9 10 3 11 4 6 14 20 18 13 9 2 4 15 22 17 16 8 5 7 10 6 12 9 14 3 2 5 8 4 6 1 9 12 1 1 1 7 2 3 3 4 4 3 4 5 5 2 6 6 3 1 6 7 2 6 5 5
Sample Output 3
3 1 11 34 36 29 15 1 6 9 15 15
Sample Input 4
12 12 5 18 7 21 9 30 12 25 6 17 11 4 14 40 33 28 19 45 37 31 22 26 35 8 10 29 55 48 42 60 51 46 39 44 20 13 16 34 58 70 65 72 69 63 57 50 27 15 23 41 62 80 95 90 88 76 61 53 32 18 9 36 59 84 100 120 110 92 74 56 38 24 20 43 67 89 115 130 125 101 83 64 47 28 12 31 54 78 96 112 105 87 71 49 30 19 8 27 45 66 79 91 86 73 58 42 25 14 6 22 39 52 68 77 70 55 43 33 16 10 11 24 30 37 46 59 48 40 29 21 13 7 3 15 9 26 12 34 18 23 5 20 8 2 20 1 1 1 12 12 1 12 12 6 6 7 7 5 8 8 5 3 4 10 10 2 2 11 11 4 9 9 4 6 1 1 6 12 9 7 12 5 5 5 5
Sample Output 4
5 4 3 2 408 448 240 271 97 59 54 20 124 146 9 30 5 28 230 230
Sample Input 5
1 1 1000000000 5 1 1 1 1 1 1 1 1 1 1
Sample Output 5
1000000000 1000000000 1000000000 1000000000 1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君はダンジョンに挑みます。ダンジョンには N 個の部屋が一列に並んでおり、入口に近い方から順に部屋 1, 部屋 2, \ldots, 部屋 N と番号がついています。各部屋にはモンスターが 1 体ずついます。
高橋君がダンジョンに挑む際は、部屋 1 から順に部屋 2, 部屋 3, \ldots と一つずつ進み、各部屋で必ずモンスターと戦います。途中の部屋を飛ばすことはできません。部屋 i のモンスターを倒すには、戦闘前の時点で戦闘力が A_i 以上必要です。モンスターを倒すと、戦闘力が B_i 増加します。モンスターを倒せなかった場合、高橋君はそこで探索を終了します。すべてのモンスターを倒した場合も探索は終了します。
Q 個のイベントが順に与えられます。イベントには次の 2 種類があります。
1 i a b:部屋 i のモンスターの情報が更新され、以後、倒すために必要な戦闘力が a に、倒したときに増加する戦闘力が b に変わります。2 s:高橋君が初期戦闘力 s で部屋 1 からダンジョンに挑みます。その時点での部屋の情報に基づいて、高橋君が倒すことができるモンスターの数を求めてください。
種類 2 の各挑戦は互いに独立です。挑戦によって部屋の情報が変化することはなく、部屋の情報を変更するのは種類 1 のイベントのみです。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 10^5
- N + Q \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9
- 0 \leq B_i \leq 10^9
- 種類
1のイベントにおいて、1 \leq i \leq N、0 \leq a \leq 10^9、0 \leq b \leq 10^9 - 種類
2のイベントにおいて、0 \leq s \leq 10^9 - 種類
2のイベントは 1 つ以上存在する - 入力はすべて整数である
入力
N Q
A_1 B_1
A_2 B_2
\vdots
A_N B_N
\mathrm{event}_1
\mathrm{event}_2
\vdots
\mathrm{event}_Q
1 行目には、部屋の個数 N とイベントの個数 Q がスペース区切りで与えられます。続く N 行のうち i 行目には、部屋 i のモンスターを倒すために必要な戦闘力 A_i と、倒したときに増加する戦闘力 B_i がスペース区切りで与えられます。続く Q 行では、イベントが次のいずれかの形式で与えられます。
1 i a b2 s
出力
種類 2 のイベントごとに、高橋君が倒すことができるモンスターの数を、イベントが与えられた順に 1 行ずつ出力してください。
入力例 1
4 5 3 2 5 3 10 1 9 5 2 3 2 4 1 3 8 10 2 3 2 0
出力例 1
2 2 4 0
入力例 2
3 6 0 0 1 5 6 0 2 0 2 1 1 2 2 10 2 1 1 1 5 0 2 5
出力例 2
1 3 1 3
入力例 3
8 10 5 3 8 2 7 10 20 1 21 5 30 0 25 8 40 10 2 5 2 4 1 6 26 5 2 5 1 8 39 1 2 5 1 1 6 100 2 5 1 4 200 0 2 100
出力例 3
5 0 7 8 0 8
入力例 4
15 18 10 5 12 0 12 8 25 10 40 20 70 0 60 15 80 5 90 30 130 10 150 50 210 0 205 10 220 100 400 1 2 10 2 9 1 2 20 100 2 10 1 6 150 100 2 10 1 5 200 0 2 10 1 5 35 5 1 12 300 0 2 50 1 15 260 0 2 50 1 1 0 0 2 0 1 14 1000000000 0 2 1000000000 2 399
出力例 4
3 0 1 1 1 15 15 1 15 13
入力例 5
1 7 0 0 2 0 1 1 1000000000 1000000000 2 999999999 2 1000000000 1 1 0 1000000000 2 0 2 1
出力例 5
1 0 1 1 1
Score : 466 pts
Problem Statement
Takahashi challenges a dungeon. The dungeon has N rooms arranged in a row, numbered room 1, room 2, \ldots, room N in order from the entrance. Each room contains exactly one monster.
When Takahashi challenges the dungeon, he proceeds one room at a time starting from room 1, then room 2, room 3, \ldots, and must fight the monster in each room. He cannot skip any rooms. To defeat the monster in room i, his combat power before the battle must be at least A_i. Upon defeating a monster, his combat power increases by B_i. If he fails to defeat a monster, Takahashi ends his exploration there. The exploration also ends if he defeats all monsters.
Q events are given in order. There are two types of events:
1 i a b: The information of the monster in room i is updated. From now on, the combat power required to defeat it becomes a, and the combat power gained upon defeating it becomes b.2 s: Takahashi challenges the dungeon starting from room 1 with an initial combat power of s. Based on the room information at that point, determine the number of monsters Takahashi can defeat.
Each challenge of type 2 is independent of the others. A challenge does not change the room information; only type 1 events modify room information.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 10^5
- N + Q \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9
- 0 \leq B_i \leq 10^9
- In type
1events, 1 \leq i \leq N, 0 \leq a \leq 10^9, 0 \leq b \leq 10^9 - In type
2events, 0 \leq s \leq 10^9 - There is at least one type
2event - All input values are integers
Input
N Q
A_1 B_1
A_2 B_2
\vdots
A_N B_N
\mathrm{event}_1
\mathrm{event}_2
\vdots
\mathrm{event}_Q
The first line contains the number of rooms N and the number of events Q, separated by a space. The following N lines each contain, for the i-th line, the combat power A_i required to defeat the monster in room i and the combat power increase B_i upon defeating it, separated by a space. The following Q lines each describe an event in one of the following formats:
1 i a b2 s
Output
For each type 2 event, output the number of monsters Takahashi can defeat, one per line, in the order the events are given.
Sample Input 1
4 5 3 2 5 3 10 1 9 5 2 3 2 4 1 3 8 10 2 3 2 0
Sample Output 1
2 2 4 0
Sample Input 2
3 6 0 0 1 5 6 0 2 0 2 1 1 2 2 10 2 1 1 1 5 0 2 5
Sample Output 2
1 3 1 3
Sample Input 3
8 10 5 3 8 2 7 10 20 1 21 5 30 0 25 8 40 10 2 5 2 4 1 6 26 5 2 5 1 8 39 1 2 5 1 1 6 100 2 5 1 4 200 0 2 100
Sample Output 3
5 0 7 8 0 8
Sample Input 4
15 18 10 5 12 0 12 8 25 10 40 20 70 0 60 15 80 5 90 30 130 10 150 50 210 0 205 10 220 100 400 1 2 10 2 9 1 2 20 100 2 10 1 6 150 100 2 10 1 5 200 0 2 10 1 5 35 5 1 12 300 0 2 50 1 15 260 0 2 50 1 1 0 0 2 0 1 14 1000000000 0 2 1000000000 2 399
Sample Output 4
3 0 1 1 1 15 15 1 15 13
Sample Input 5
1 7 0 0 2 0 1 1 1000000000 1000000000 2 999999999 2 1000000000 1 1 0 1000000000 2 0 2 1
Sample Output 5
1 0 1 1 1