Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君は、あるバス停の時刻表を分析しています。
このバス停には 1 日の間にちょうど N 本のバスが到着します。各バスの到着時刻は、1 日の開始時点(午前 0 時)を 0 秒として、0 以上 86400 以下の整数(秒)で記録されています。
N 本のバスの到着時刻は T_1, T_2, \ldots, T_N として時刻の早い順に与えられ、同じ時刻に 2 本以上のバスが到着することはありません(すなわち T_1 < T_2 < \cdots < T_N です)。
高橋君は、時刻順で連続する 2 本のバスの到着間隔をすべて求め、その中で最も短い間隔と最も長い間隔を知りたいと思っています。具体的には、i 番目と i+1 番目のバスの到着間隔を T_{i+1} - T_i(秒)と定義します(1 \leq i \leq N-1)。
これら N-1 個の到着間隔のうち、最小値と最大値をそれぞれ求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq T_i \leq 86400
- T_1 < T_2 < \cdots < T_N
- 入力はすべて整数である。
入力
N T_1 T_2 \ldots T_N
- 1 行目には、バスの本数を表す整数 N が与えられる。
- 2 行目には、各バスの到着時刻を表す N 個の整数 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。T_i は i 番目に到着するバスの到着時刻(秒)を表す。
出力
到着間隔の最小値と最大値を、スペース区切りで 1 行に出力せよ。
入力例 1
5 100 200 500 700 800
出力例 1
100 300
入力例 2
3 0 43200 86400
出力例 2
43200 43200
入力例 3
10 60 120 300 1800 3600 7200 36000 72000 80000 86400
出力例 3
60 36000
Score : 233 pts
Problem Statement
Takahashi is analyzing the timetable of a certain bus stop.
Exactly N buses arrive at this bus stop during one day. The arrival time of each bus is recorded as an integer (in seconds) between 0 and 86400 inclusive, where 0 represents the start of the day (midnight, 12:00 AM).
The arrival times of the N buses are given as T_1, T_2, \ldots, T_N in chronological order, and no two or more buses arrive at the same time (that is, T_1 < T_2 < \cdots < T_N).
Takahashi wants to find all the intervals between consecutive buses in chronological order, and determine the shortest and longest intervals among them. Specifically, the interval between the i-th and (i+1)-th buses is defined as T_{i+1} - T_i (seconds) (1 \leq i \leq N-1).
Find the minimum and maximum values among these N-1 intervals.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq T_i \leq 86400
- T_1 < T_2 < \cdots < T_N
- All input values are integers.
Input
N T_1 T_2 \ldots T_N
- The first line contains an integer N representing the number of buses.
- The second line contains N integers T_1, T_2, \ldots, T_N separated by spaces, representing the arrival times of each bus. T_i represents the arrival time (in seconds) of the i-th bus to arrive.
Output
Print the minimum and maximum values of the intervals, separated by a space, on one line.
Sample Input 1
5 100 200 500 700 800
Sample Output 1
100 300
Sample Input 2
3 0 43200 86400
Sample Output 2
43200 43200
Sample Input 3
10 60 120 300 1800 3600 7200 36000 72000 80000 86400
Sample Output 3
60 36000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は旅行の計画を立てています。旅行先の候補として N 箇所の観光地があり、そのうち 0 箇所以上 K 箇所以下を選んで訪れることができます。ただし、同じ観光地を複数回選ぶことはできません。
各観光地 i( i = 1, 2, \ldots, N )にはお土産屋があり、 C_i 個のお土産が売られています。観光地 i の j 番目( j = 1, 2, \ldots, C_i )のお土産には満足度 V_{i,j} が定められています。満足度は負の値をとることもあります。
各観光地 i には評価値が定められています。評価値は、その観光地で売られている C_i 個のお土産の満足度の中央値です。ここで、中央値とは、 C_i 個の満足度を昇順に並べたときの \frac{C_i + 1}{2} 番目の値を指します。 C_i は必ず奇数であるため、中央値は一意に定まり、整数になります。なお、評価値が負の値になることもあります。
高橋君は、訪れる観光地をうまく選ぶことで、選んだ観光地の評価値の合計を最大化したいと考えています。どの観光地も訪れない場合、評価値の合計は 0 とします。
高橋君の代わりに、評価値の合計の最大値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq K \leq N
- 1 \leq C_i \leq 99
- C_i は奇数
- -1000 \leq V_{i,j} \leq 1000
- 入力はすべて整数
入力
N K
C_1
V_{1,1} V_{1,2} \ldots V_{1,C_1}
C_2
V_{2,1} V_{2,2} \ldots V_{2,C_2}
\vdots
C_N
V_{N,1} V_{N,2} \ldots V_{N,C_N}
- 1 行目には、観光地の数を表す整数 N と、訪れることができる最大箇所数を表す整数 K が、スペース区切りで与えられる。
- 続いて、各観光地 i ( i = 1, 2, \ldots, N )について 2 行ずつ与えられる。
- 1 行目には、観光地 i のお土産の個数を表す奇数 C_i が与えられる。
- 2 行目には、観光地 i の各お土産の満足度を表す整数 V_{i,1}, V_{i,2}, \ldots, V_{i,C_i} がスペース区切りで与えられる。
出力
訪れる観光地を 0 箇所以上 K 箇所以下選んだときの、選んだ観光地の評価値の合計の最大値を 1 行に出力せよ。
入力例 1
3 2 3 3 1 2 3 5 -1 10 3 -3 -5 -1
出力例 1
7
入力例 2
3 2 3 -5 -3 -7 3 -10 -1 -4 3 -2 -8 -6
出力例 2
0
入力例 3
5 3 5 10 20 30 5 15 3 -2 3 1 7 100 -50 0 25 75 50 -10 3 -1 -2 -3 3 7 8 9
出力例 3
48
入力例 4
10 5 5 12 -3 7 20 -8 3 100 200 150 7 -5 -10 3 8 -1 0 6 1 -1000 9 50 40 30 20 10 0 -10 -20 -30 3 999 998 1000 5 -100 -200 -300 -400 -500 1 500 3 0 0 0 5 1 2 3 4 5
出力例 4
1666
入力例 5
1 1 1 -5
出力例 5
0
Score : 300 pts
Problem Statement
Takahashi is planning a trip. There are N candidate tourist spots, and he can choose to visit between 0 and K of them (inclusive). However, he cannot choose the same tourist spot more than once.
Each tourist spot i (i = 1, 2, \ldots, N) has a souvenir shop that sells C_i souvenirs. The j-th souvenir (j = 1, 2, \ldots, C_i) at tourist spot i has a satisfaction value V_{i,j}. Satisfaction values can be negative.
Each tourist spot i has a rating. The rating is the median of the satisfaction values of the C_i souvenirs sold at that tourist spot. Here, the median refers to the \frac{C_i + 1}{2}-th value when the C_i satisfaction values are sorted in ascending order. Since C_i is always odd, the median is uniquely determined and is an integer. Note that the rating can be negative.
Takahashi wants to maximize the sum of the ratings of the tourist spots he visits by choosing them wisely. If he visits no tourist spots, the sum of ratings is 0.
On behalf of Takahashi, find the maximum possible sum of ratings.
Constraints
- 1 \leq N \leq 100
- 1 \leq K \leq N
- 1 \leq C_i \leq 99
- C_i is odd
- -1000 \leq V_{i,j} \leq 1000
- All input values are integers
Input
N K
C_1
V_{1,1} V_{1,2} \ldots V_{1,C_1}
C_2
V_{2,1} V_{2,2} \ldots V_{2,C_2}
\vdots
C_N
V_{N,1} V_{N,2} \ldots V_{N,C_N}
- The first line contains an integer N representing the number of tourist spots and an integer K representing the maximum number of spots that can be visited, separated by a space.
- Then, for each tourist spot i (i = 1, 2, \ldots, N), two lines are given:
- The first line contains an odd number C_i representing the number of souvenirs at tourist spot i.
- The second line contains integers V_{i,1}, V_{i,2}, \ldots, V_{i,C_i} representing the satisfaction values of each souvenir at tourist spot i, separated by spaces.
Output
Output in one line the maximum possible sum of ratings when choosing between 0 and K tourist spots (inclusive) to visit.
Sample Input 1
3 2 3 3 1 2 3 5 -1 10 3 -3 -5 -1
Sample Output 1
7
Sample Input 2
3 2 3 -5 -3 -7 3 -10 -1 -4 3 -2 -8 -6
Sample Output 2
0
Sample Input 3
5 3 5 10 20 30 5 15 3 -2 3 1 7 100 -50 0 25 75 50 -10 3 -1 -2 -3 3 7 8 9
Sample Output 3
48
Sample Input 4
10 5 5 12 -3 7 20 -8 3 100 200 150 7 -5 -10 3 8 -1 0 6 1 -1000 9 50 40 30 20 10 0 -10 -20 -30 3 999 998 1000 5 -100 -200 -300 -400 -500 1 500 3 0 0 0 5 1 2 3 4 5
Sample Output 4
1666
Sample Input 5
1 1 1 -5
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、ある都市の建物と道路の関係を調査しています。
都市は縦 H 行、横 W 列のマス目で表されます。上から i 行目(1 \leq i \leq H)、左から j 列目(1 \leq j \leq W)のマスを (i, j) と表記します。各マスは次のいずれかの種類を持ちます。
B:建物R:道路E:空き地
初期状態において、マス (i, j) の種類は文字列 S_i の j 文字目で与えられます。
ある建物のマスが、上下左右に隣り合うマスのうち少なくとも 1 つが道路のマスであるとき、その建物のマスは 道路に面している といいます。斜めに隣り合っている場合は、道路に面しているとはみなしません。
これから Q 回の道路工事が順に行われます。工事の効果は累積します(一度道路に変わったマスは、以降の工事を通じて道路のままです)。
k 回目(1 \leq k \leq Q)の工事では、行 U_k から行 D_k まで、かつ列 L_k から列 R_k までの長方形領域内にあるすべての空き地のマスが道路に変わります。すでに道路であるマスや建物のマスは変化しません。工事範囲外のマスも変化しません。
各工事の長方形領域内に建物のマスは含まれないことが保証されます。
各工事の直後に、道路に面している建物のマスの総数を求めてください。1 つの建物のマスが複数の道路マスと隣り合っていても、1 つとして数えます。
制約
- 1 \leq H, W \leq 10^6
- 1 \leq Q \leq 10^5
- H W \leq 2 \times 10^6
- H + Q \leq 2 \times 10^5
- S_i(1 \leq i \leq H)は長さ W の文字列であり、各文字は
B、R、Eのいずれかである - 1 \leq U_k \leq D_k \leq H(1 \leq k \leq Q)
- 1 \leq L_k \leq R_k \leq W(1 \leq k \leq Q)
- \displaystyle \sum_{k=1}^{Q} \left( 2(D_k-U_k+1) + 2(R_k-L_k+1) \right) \leq 5 \times 10^6
- 各工事範囲の長方形領域内に
Bであるマスは含まれない - 入力はすべて整数および文字列である
入力
H W Q S_1 S_2 \vdots S_H U_1 D_1 L_1 R_1 U_2 D_2 L_2 R_2 \vdots U_Q D_Q L_Q R_Q
- 1 行目には、都市の行数を表す整数 H、列数を表す整数 W、道路工事の回数を表す整数 Q がスペース区切りで与えられる。
- 続く H 行では、都市の初期状態を表す文字列 S_i(1 \leq i \leq H)が与えられる。S_i の j 文字目がマス (i, j) の初期状態を表す。
- 続く Q 行では、道路工事の情報が与えられる。k 回目の工事を表す行には、工事範囲の上端の行番号 U_k、下端の行番号 D_k、左端の列番号 L_k、右端の列番号 R_k がスペース区切りで与えられる。工事範囲は、行 U_k 以上 D_k 以下かつ列 L_k 以上 R_k 以下の長方形領域である。
出力
A_1 A_2 \vdots A_Q
Q 行出力せよ。k 行目には、k 回目の工事の直後における、道路に面している建物のマスの総数 A_k を出力してください。
入力例 1
4 5 3 EEEEE EBERE EEEEE REBEE 1 1 1 3 3 4 4 5 2 3 3 3
出力例 1
1 2 2
入力例 2
3 3 4 BEB EEE BEB 2 2 2 2 1 3 2 2 2 2 1 3 1 1 2 2
出力例 2
0 4 4 4
入力例 3
8 10 7 EEREEEEEER EEBEEEEEEE EEEEERBEEE EEEEEEEEEE BEEEEEEEER EEEEREEEEE EEEEBEEEEE REEEEEEEEE 1 1 1 5 4 4 1 10 2 3 4 6 5 6 2 4 6 8 6 8 1 3 8 10 7 8 1 3
出力例 3
3 4 4 4 4 4 4
入力例 4
12 15 10 EEEEREEEEEEEEEE EEEEBEEEREEBEEE EEEEEEEEEEEEEEE EEBEEEEEEEEEEER EREEEEEEEBEEEEE EEEEEEEEEEEEERE EEEEEEBEEEEEEBE EEEEEEEEEEEEEEE BEEEEEEBEEEEEER EEEEREEEEEEEEEE EEEBEEEEEEBEEEE REEEEEEEEEEEEER 1 1 1 15 2 8 1 1 3 6 4 8 2 4 13 15 5 6 11 15 8 10 2 4 10 12 12 15 6 8 8 13 3 3 1 15 12 12 1 15
出力例 4
3 4 6 6 7 8 9 10 10 10
入力例 5
1 1 1 E 1 1 1 1
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is investigating the relationship between buildings and roads in a certain city.
The city is represented as a grid with H rows and W columns. The cell at the i-th row from the top (1 \leq i \leq H) and the j-th column from the left (1 \leq j \leq W) is denoted as (i, j). Each cell has one of the following types:
B: BuildingR: RoadE: Empty lot
In the initial state, the type of cell (i, j) is given by the j-th character of the string S_i.
A building cell is said to be facing a road if at least one of its vertically or horizontally adjacent cells is a road cell. Diagonally adjacent cells are not considered as facing a road.
From now on, Q road construction operations will be performed in order. The effects of construction are cumulative (once a cell becomes a road, it remains a road throughout all subsequent operations).
In the k-th operation (1 \leq k \leq Q), all empty lot cells within the rectangular region from row U_k to row D_k and from column L_k to column R_k are converted to road cells. Cells that are already roads or buildings do not change. Cells outside the construction area also do not change.
It is guaranteed that no building cells are contained within the rectangular region of each construction operation.
After each construction operation, determine the total number of building cells that are facing a road. Even if a single building cell is adjacent to multiple road cells, it is counted as one.
Constraints
- 1 \leq H, W \leq 10^6
- 1 \leq Q \leq 10^5
- H W \leq 2 \times 10^6
- H + Q \leq 2 \times 10^5
- S_i (1 \leq i \leq H) is a string of length W, where each character is one of
B,R, orE - 1 \leq U_k \leq D_k \leq H (1 \leq k \leq Q)
- 1 \leq L_k \leq R_k \leq W (1 \leq k \leq Q)
- \displaystyle \sum_{k=1}^{Q} \left( 2(D_k-U_k+1) + 2(R_k-L_k+1) \right) \leq 5 \times 10^6
- No cell with
Bis contained within the rectangular region of each construction operation - All inputs are integers and strings
Input
H W Q S_1 S_2 \vdots S_H U_1 D_1 L_1 R_1 U_2 D_2 L_2 R_2 \vdots U_Q D_Q L_Q R_Q
- The first line contains integers H representing the number of rows, W representing the number of columns, and Q representing the number of road construction operations, separated by spaces.
- The following H lines give the strings S_i (1 \leq i \leq H) representing the initial state of the city. The j-th character of S_i represents the initial state of cell (i, j).
- The following Q lines give the information of the road construction operations. The line representing the k-th operation contains the top row number U_k, bottom row number D_k, left column number L_k, and right column number R_k of the construction area, separated by spaces. The construction area is the rectangular region with rows from U_k to D_k (inclusive) and columns from L_k to R_k (inclusive).
Output
A_1 A_2 \vdots A_Q
Output Q lines. On the k-th line, output A_k, the total number of building cells that are facing a road immediately after the k-th construction operation.
Sample Input 1
4 5 3 EEEEE EBERE EEEEE REBEE 1 1 1 3 3 4 4 5 2 3 3 3
Sample Output 1
1 2 2
Sample Input 2
3 3 4 BEB EEE BEB 2 2 2 2 1 3 2 2 2 2 1 3 1 1 2 2
Sample Output 2
0 4 4 4
Sample Input 3
8 10 7 EEREEEEEER EEBEEEEEEE EEEEERBEEE EEEEEEEEEE BEEEEEEEER EEEEREEEEE EEEEBEEEEE REEEEEEEEE 1 1 1 5 4 4 1 10 2 3 4 6 5 6 2 4 6 8 6 8 1 3 8 10 7 8 1 3
Sample Output 3
3 4 4 4 4 4 4
Sample Input 4
12 15 10 EEEEREEEEEEEEEE EEEEBEEEREEBEEE EEEEEEEEEEEEEEE EEBEEEEEEEEEEER EREEEEEEEBEEEEE EEEEEEEEEEEEERE EEEEEEBEEEEEEBE EEEEEEEEEEEEEEE BEEEEEEBEEEEEER EEEEREEEEEEEEEE EEEBEEEEEEBEEEE REEEEEEEEEEEEER 1 1 1 15 2 8 1 1 3 6 4 8 2 4 13 15 5 6 11 15 8 10 2 4 10 12 12 15 6 8 8 13 3 3 1 15 12 12 1 15
Sample Output 4
3 4 6 6 7 8 9 10 10 10
Sample Input 5
1 1 1 E 1 1 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
ある通りに N 本の街灯が一列に並んでいます。
i 番目の街灯のはじめの明るさは A_i です。明るさは 1 日あたり D_i ずつ低下しますが、0 未満にはなりません。すなわち、t 日後の i 番目の街灯の明るさは
C_i(t) = \max(0,\ A_i - D_i \times t)
です。
t 日後の時点で明るさが K 以下である街灯を「暗い街灯」と呼びます。
t 日後における暗い部分区間の個数を、次のように定義します。1 \leq l \leq r \leq N を満たす整数の組 (l, r) であって、l 番目から r 番目までのすべての街灯が t 日後に暗い街灯であるものの個数です。
ここで (l, r) は極大な連続区間である必要はなく、条件を満たす組をすべて数えます。たとえば、連続する m 本の街灯がすべて暗い街灯であるとき、その中から選べる組 (l, r) は \frac{m(m+1)}{2} 個あり、それらすべてを数えます。
Q 個の問い合わせが与えられます。j 番目の問い合わせでは、T_j 日後における暗い部分区間の個数を求めてください。
制約
- 1 \leq N, Q
- N + Q \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq T_j \leq 10^9 (1 \leq j \leq Q)
- 入力はすべて整数である
- 各問い合わせに対する答えは 64 ビット符号付き整数の範囲に収まる
入力
N Q K A_1 D_1 A_2 D_2 \vdots A_N D_N T_1 T_2 \vdots T_Q
- 1 行目には、街灯の本数を表す N、問い合わせの数を表す Q、暗いと判定する明るさの閾値 K が、スペース区切りで与えられる。
- 続く N 行では、各街灯の情報が与えられる。
- 1 + i 行目には、i 番目の街灯のはじめの明るさ A_i と、1 日あたりに低下する明るさ D_i が、スペース区切りで与えられる。
- 続く Q 行では、問い合わせが与えられる。
- 1 + N + j 行目には、j 番目の問い合わせの日数 T_j が与えられる。
出力
Q 行出力せよ。
j 行目には、T_j 日後における暗い部分区間の個数を出力せよ。
入力例 1
5 4 10 15 2 8 0 30 5 10 1 25 3 0 2 4 10
出力例 1
2 2 10 15
入力例 2
4 5 5 6 0 5 0 20 10 1 0 0 1 2 3 10
出力例 2
2 2 6 6 6
入力例 3
10 8 20 50 5 18 0 100 10 21 1 0 0 35 3 80 20 20 0 60 4 25 2 0 1 2 3 5 7 10 20
出力例 3
3 5 5 8 17 19 55 55
入力例 4
25 12 100 150 10 90 0 300 20 101 1 500 50 0 0 250 5 120 30 100 0 1000 100 80 2 450 25 600 0 110 3 700 70 95 1 130 15 100 5 1000000000 100000000 400 40 200 0 105 2 10000 500 99 0 160 8 0 1 2 3 5 7 10 15 20 50 100 1000000000
出力例 4
7 10 14 15 18 18 63 68 74 116 116 116
入力例 5
1 1 0 1000000000 1000000000 1000000000
出力例 5
1
Score : 400 pts
Problem Statement
There are N street lights lined up in a row along a certain street.
The initial brightness of the i-th street light is A_i. The brightness decreases by D_i per day, but does not go below 0. That is, the brightness of the i-th street light after t days is
C_i(t) = \max(0,\ A_i - D_i \times t)
A street light whose brightness is K or less at time t days later is called a "dark street light."
The number of dark subintervals after t days is defined as follows: it is the number of pairs of integers (l, r) satisfying 1 \leq l \leq r \leq N such that all street lights from the l-th to the r-th are dark street lights after t days.
Here, (l, r) does not need to be a maximal contiguous interval; all pairs satisfying the condition are counted. For example, if m consecutive street lights are all dark, the number of pairs (l, r) that can be chosen from them is \frac{m(m+1)}{2}, and all of them are counted.
Q queries are given. For the j-th query, find the number of dark subintervals after T_j days.
Constraints
- 1 \leq N, Q
- N + Q \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq T_j \leq 10^9 (1 \leq j \leq Q)
- All inputs are integers
- The answer to each query fits in a 64-bit signed integer
Input
N Q K A_1 D_1 A_2 D_2 \vdots A_N D_N T_1 T_2 \vdots T_Q
- The first line contains N representing the number of street lights, Q representing the number of queries, and K representing the brightness threshold for determining darkness, separated by spaces.
- The following N lines give information about each street light.
- The (1 + i)-th line contains the initial brightness A_i of the i-th street light and the brightness decrease per day D_i, separated by a space.
- The following Q lines give the queries.
- The (1 + N + j)-th line contains the number of days T_j for the j-th query.
Output
Output Q lines.
On the j-th line, output the number of dark subintervals after T_j days.
Sample Input 1
5 4 10 15 2 8 0 30 5 10 1 25 3 0 2 4 10
Sample Output 1
2 2 10 15
Sample Input 2
4 5 5 6 0 5 0 20 10 1 0 0 1 2 3 10
Sample Output 2
2 2 6 6 6
Sample Input 3
10 8 20 50 5 18 0 100 10 21 1 0 0 35 3 80 20 20 0 60 4 25 2 0 1 2 3 5 7 10 20
Sample Output 3
3 5 5 8 17 19 55 55
Sample Input 4
25 12 100 150 10 90 0 300 20 101 1 500 50 0 0 250 5 120 30 100 0 1000 100 80 2 450 25 600 0 110 3 700 70 95 1 130 15 100 5 1000000000 100000000 400 40 200 0 105 2 10000 500 99 0 160 8 0 1 2 3 5 7 10 15 20 50 100 1000000000
Sample Output 4
7 10 14 15 18 18 63 68 74 116 116 116
Sample Input 5
1 1 0 1000000000 1000000000 1000000000
Sample Output 5
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は、N 件の取引が順番に記録された家計簿を管理しています。i 番目の取引には種類 D_i と金額 B_i が書かれています。
- 種類が
Rのとき、その金額を残高に加えます(入金)。 - 種類が
Lのとき、その金額を残高から引きます(出金)。
高橋君は Q 個の操作を入力順に処理します。操作は以下の 2 種類です。
1 p D b:p 番目の取引を、種類 D、金額 b に修正する。2 K:現在の家計簿について、残高を 0 として 1 番目から順に各取引を処理したとき、取引を処理した直後の残高が初めて K 以上になる取引の番号を答える。どの取引の処理後にも残高が K 以上にならない場合は 0 を答える。
ここで、i 番目の取引の番号は i です。残高は途中で負になることもあります。
タイプ 2 の操作は質問であり、家計簿の内容は変化しません。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 操作全体に含まれるタイプ 2 の操作の個数を M とすると、1 \leq M かつ N+Q+M \leq 2 \times 10^5
- D_i \in \{\texttt{R}, \texttt{L}\}
- 1 \leq B_i \leq 10^9
- B_i は整数
- タイプ 1 の操作について:
- 1 \leq p \leq N
- D \in \{\texttt{R}, \texttt{L}\}
- 1 \leq b \leq 10^9
- b は整数
- タイプ 2 の操作について:
- 1 \leq K \leq 2 \times 10^{14}
- K は整数
入力
N Q
D_1 B_1
D_2 B_2
:
D_N B_N
\mathrm{Query}_1
\mathrm{Query}_2
:
\mathrm{Query}_Q
- 1 行目には、取引の件数 N と操作の個数 Q が、スペース区切りで与えられる。
- 続く N 行のうち i 行目には、i 番目の取引の種類 D_i と金額 B_i が、スペース区切りで与えられる。
- 続く Q 行には、操作が以下のいずれかの形式で与えられる。
1 p D b
2 K
1 p D bは、p 番目の取引を種類 D(RまたはL)、金額 b に修正する操作を表す。2 Kは、目標残高 K に対する質問を表す。
出力
タイプ 2 の操作ごとに、答えを 1 行に出力してください。
入力例 1
5 6 R 10 L 3 R 5 L 20 R 15 2 7 2 12 1 4 L 4 2 12 1 2 R 2 2 20
出力例 1
1 3 3 5
入力例 2
3 4 L 10 R 5 L 1 2 1 2 100 1 2 L 5 2 1
出力例 2
0 0 0
入力例 3
10 12 R 8 L 4 R 15 L 10 R 7 R 3 L 20 R 25 L 5 R 6 2 10 2 20 1 7 L 5 2 30 1 4 R 12 2 40 1 1 L 3 2 5 2 50 1 10 L 2 2 25 2 60
出力例 3
3 8 8 6 3 8 5 0
入力例 4
30 30 R 100 L 40 R 70 L 150 R 200 L 30 L 80 R 300 L 120 R 50 R 400 L 500 R 600 L 100 R 90 L 250 R 700 L 60 R 30 L 20 R 1000 L 800 R 450 L 300 R 200 L 100 R 50 L 400 R 900 L 700 2 100 2 500 1 4 R 150 2 500 1 12 L 100 2 1000 1 22 R 800 2 2000 1 28 R 400 2 2500 1 1 L 100 2 50 2 3000 1 17 L 700 2 1500 1 30 R 700 2 3500 1 7 R 80 2 1200 1 21 L 1000 2 1000 1 5 L 200 2 800 1 13 L 600 2 700 1 2 R 40 2 600 1 29 L 900 2 400 2 5000
出力例 4
1 11 8 11 21 21 4 22 22 29 13 13 13 30 11 11 0
入力例 5
1 1 R 1000000000 2 200000000000000
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi manages a household budget book in which N transactions are recorded in order. The i-th transaction has a type D_i and an amount B_i.
- If the type is
R, the amount is added to the balance (deposit). - If the type is
L, the amount is subtracted from the balance (withdrawal).
Takahashi processes Q operations in the order they are given. There are 2 types of operations:
1 p D b: Modify the p-th transaction to have type D and amount b.2 K: For the current state of the budget book, starting with a balance of 0 and processing each transaction from the 1-st onward, answer the number of the first transaction after which the balance becomes at least K. If the balance never reaches K or more after any transaction, answer 0.
Here, the number of the i-th transaction is i. The balance may become negative during processing.
Type 2 operations are queries and do not change the contents of the budget book.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- Let M be the number of type 2 operations among all operations. Then 1 \leq M and N+Q+M \leq 2 \times 10^5.
- D_i \in \{\texttt{R}, \texttt{L}\}
- 1 \leq B_i \leq 10^9
- B_i is an integer
- For type 1 operations:
- 1 \leq p \leq N
- D \in \{\texttt{R}, \texttt{L}\}
- 1 \leq b \leq 10^9
- b is an integer
- For type 2 operations:
- 1 \leq K \leq 2 \times 10^{14}
- K is an integer
Input
N Q
D_1 B_1
D_2 B_2
:
D_N B_N
\mathrm{Query}_1
\mathrm{Query}_2
:
\mathrm{Query}_Q
- The first line contains the number of transactions N and the number of operations Q, separated by a space.
- In the following N lines, the i-th line contains the type D_i and amount B_i of the i-th transaction, separated by a space.
- The following Q lines each contain an operation in one of the following formats:
1 p D b
2 K
1 p D brepresents an operation that modifies the p-th transaction to type D (RorL) and amount b.2 Krepresents a query for the target balance K.
Output
For each type 2 operation, output the answer on a single line.
Sample Input 1
5 6 R 10 L 3 R 5 L 20 R 15 2 7 2 12 1 4 L 4 2 12 1 2 R 2 2 20
Sample Output 1
1 3 3 5
Sample Input 2
3 4 L 10 R 5 L 1 2 1 2 100 1 2 L 5 2 1
Sample Output 2
0 0 0
Sample Input 3
10 12 R 8 L 4 R 15 L 10 R 7 R 3 L 20 R 25 L 5 R 6 2 10 2 20 1 7 L 5 2 30 1 4 R 12 2 40 1 1 L 3 2 5 2 50 1 10 L 2 2 25 2 60
Sample Output 3
3 8 8 6 3 8 5 0
Sample Input 4
30 30 R 100 L 40 R 70 L 150 R 200 L 30 L 80 R 300 L 120 R 50 R 400 L 500 R 600 L 100 R 90 L 250 R 700 L 60 R 30 L 20 R 1000 L 800 R 450 L 300 R 200 L 100 R 50 L 400 R 900 L 700 2 100 2 500 1 4 R 150 2 500 1 12 L 100 2 1000 1 22 R 800 2 2000 1 28 R 400 2 2500 1 1 L 100 2 50 2 3000 1 17 L 700 2 1500 1 30 R 700 2 3500 1 7 R 80 2 1200 1 21 L 1000 2 1000 1 5 L 200 2 800 1 13 L 600 2 700 1 2 R 40 2 600 1 29 L 900 2 400 2 5000
Sample Output 4
1 11 8 11 21 21 4 22 22 29 13 13 13 30 11 11 0
Sample Input 5
1 1 R 1000000000 2 200000000000000
Sample Output 5
0