Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
ある OS のバージョンは古い順に "Ocelot", "Serval", "Lynx" です。
バージョン X がバージョン Y 以降のバージョンであるか判定してください。
なお、バージョン X 自身もバージョン X 以降のバージョンであるものとします。
制約
- X,Y は "
Ocelot", "Serval", "Lynx" のいずれか (引用符を含まない)
入力
入力は以下の形式で標準入力から与えられる。
X Y
出力
バージョン X がバージョン Y 以降のバージョンであれば Yes 、そうでなければ No と出力せよ。
入力例 1
Serval Ocelot
出力例 1
Yes
バージョン Serval はバージョン Ocelot 以降のバージョンです。そのため、 Yes と出力します。
入力例 2
Serval Lynx
出力例 2
No
バージョン Serval はバージョン Lynx 以降のバージョンではありません。そのため、 No と出力します。
入力例 3
Ocelot Ocelot
出力例 3
Yes
バージョン Ocelot 自身もバージョン Ocelot 以降のバージョンです。そのため、 Yes と出力します。
Score : 100 points
Problem Statement
The versions of a certain OS in chronological order are "Ocelot", "Serval", "Lynx".
Determine whether version X is the same as or newer than version Y.
Constraints
- Each of X and Y is one of "
Ocelot", "Serval", "Lynx" (without quotation marks).
Input
The input is given from Standard Input in the following format:
X Y
Output
If version X is the same as or newer than version Y, print Yes; otherwise, print No.
Sample Input 1
Serval Ocelot
Sample Output 1
Yes
Version Serval is the same as or newer than version Ocelot. Therefore, print Yes.
Sample Input 2
Serval Lynx
Sample Output 2
No
Version Serval is not the same as nor newer than version Lynx. Therefore, print No.
Sample Input 3
Ocelot Ocelot
Sample Output 3
Yes
Version Ocelot itself is the same as or newer than version Ocelot. Therefore, print Yes.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる長さ 3 以上の文字列 S が与えられます。
S はちょうど 2 種類の文字を含み、 1 文字だけ他の文字と異なります。その 1 文字を答えてください。
例えば、 S が odd なら o と答えてください。
制約
- S は英小文字からなる長さ 3 以上 10 以下の文字列
- S はちょうど 2 種類の文字を含み、 1 文字だけ他の文字と異なる
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
odd
出力例 1
o
odd のうち他の文字と異なるものは o です。
入力例 2
dad
出力例 2
a
dad のうち他の文字と異なるものは a です。
入力例 3
wwwwwwwwwv
出力例 3
v
wwwwwwwwwv のうち他の文字と異なるものは v です。
Score : 200 points
Problem Statement
You are given a string S of length at least 3 consisting of lowercase English letters.
S contains exactly two types of characters, and exactly one character is different from the others. Find that one character.
For example, if S is odd, report o.
Constraints
- S is a string of length at least 3 and at most 10 consisting of lowercase English letters.
- S contains exactly two types of characters, and exactly one character is different from the others.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
odd
Sample Output 1
o
In odd, the character different from the others is o.
Sample Input 2
dad
Sample Output 2
a
In dad, the character different from the others is a.
Sample Input 3
wwwwwwwwwv
Sample Output 3
v
In wwwwwwwwwv, the character different from the others is v.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ある OS のバージョンは N 個あり、古い順に 1,2,\dots,N の番号がついています。
PC が N 台あり、初めは i 番目の PC の OS のバージョンは i です。
Q 回の操作を順に行ってください。そのうち i 回目は次の通りです。
- 現時点での OS のバージョンが X_i かそれ以前の PC 全てを、バージョンを Y_i(>X_i) にアップグレードする。その後、この操作でアップグレードを行った PC の台数を出力する。
i<Q について、 i 回目の操作でのアップグレードが行われた状態で i+1 回目の操作に進むことに注意してください。
制約
- 入力は全て整数
- 2 \le N \le 10^6
- 1 \le Q \le 2 \times 10^5
- 1 \le X_i < Y_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
出力
Q 行出力せよ。
そのうち i 行目には、 i 回目の操作でアップグレードを行った PC の台数を出力せよ。
入力例 1
8 5 2 6 3 5 1 7 5 7 7 8
出力例 1
2 1 0 3 7
この入力には 5 回の操作が含まれます。
- はじめ、 8 台の PC のバージョンは 1,2,3,4,5,6,7,8 です。
- 1 回目の操作で、バージョンが 2 かそれ以前のバージョンの PC をバージョン 6 にアップグレードします。
- この操作によって 2 台の PC がアップグレードされ、各 PC のバージョンは 6,6,3,4,5,6,7,8 となります。
- 2 回目の操作で、バージョンが 3 かそれ以前のバージョンの PC をバージョン 5 にアップグレードします。
- この操作によって 1 台の PC がアップグレードされ、各 PC のバージョンは 6,6,5,4,5,6,7,8 となります。
- 3 回目の操作で、バージョンが 1 かそれ以前のバージョンの PC をバージョン 7 にアップグレードします。
- この操作によって 0 台の PC がアップグレードされ、各 PC のバージョンは 6,6,5,4,5,6,7,8 となります。
- 4 回目の操作で、バージョンが 5 かそれ以前のバージョンの PC をバージョン 7 にアップグレードします。
- この操作によって 3 台の PC がアップグレードされ、各 PC のバージョンは 6,6,7,7,7,6,7,8 となります。
- 5 回目の操作で、バージョンが 7 かそれ以前のバージョンの PC をバージョン 8 にアップグレードします。
- この操作によって 7 台の PC がアップグレードされ、各 PC のバージョンは 8,8,8,8,8,8,8,8 となります。
Score : 300 points
Problem Statement
There are N versions of a certain OS, numbered 1,2,\dots,N in chronological order.
There are N PCs, and initially the OS version of the i-th PC is i.
Perform Q operations in order. The i-th operation is as follows:
- Upgrade all PCs whose current OS version is X_i or earlier to version Y_i(>X_i). Then, print the number of PCs upgraded in this operation.
Note that for i<Q, the upgrades from the i-th operation are performed before proceeding to the (i+1)-th operation.
Constraints
- All input values are integers.
- 2 \le N \le 10^6
- 1 \le Q \le 2 \times 10^5
- 1 \le X_i < Y_i \le N
Input
The input is given from Standard Input in the following format:
N Q X_1 Y_1 X_2 Y_2 \vdots X_Q Y_Q
Output
Output Q lines.
The i-th line should contain the number of PCs upgraded in the i-th operation.
Sample Input 1
8 5 2 6 3 5 1 7 5 7 7 8
Sample Output 1
2 1 0 3 7
This input contains five operations.
- Initially, the versions of the eight PCs are 1,2,3,4,5,6,7,8.
- In the first operation, PCs with version 2 or earlier are upgraded to version 6.
- This operation upgrades two PCs, and the versions of the PCs become 6,6,3,4,5,6,7,8.
- In the second operation, PCs with version 3 or earlier are upgraded to version 5.
- This operation upgrades one PC, and the versions of the PCs become 6,6,5,4,5,6,7,8.
- In the third operation, PCs with version 1 or earlier are upgraded to version 7.
- This operation upgrades zero PCs, and the versions of the PCs become 6,6,5,4,5,6,7,8.
- In the fourth operation, PCs with version 5 or earlier are upgraded to version 7.
- This operation upgrades three PCs, and the versions of the PCs become 6,6,7,7,7,6,7,8.
- In the fifth operation, PCs with version 7 or earlier are upgraded to version 8.
- This operation upgrades seven PCs, and the versions of the PCs become 8,8,8,8,8,8,8,8.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
0 および 1 からなる長さ N の文字列 S が与えられます。
あなたは S に対して以下の操作を好きな回数(0 回でもよい)行うことができます。
- 先頭または末尾の 1 文字を削除し、その文字を反転して(
0ならば1に、1ならば0に変えて)、好きな位置に挿入し直す。 より厳密には、r(0)=1,r(1)=0として、以下のいずれかを行う。(ここで、S_i は S の i 文字目を表す。)- 好きな i\ (1\leq i\leq N) を選んで、S を S_2\dots S_i\,r(S_1)\,S_{i+1}\dots S_N に変更する。
- 好きな i\ (0\leq i\leq N-1) を選んで、S を S_1\dots S_i\,r(S_N)\,S_{i+1}\dots S_{N-1} に変更する。
S の全ての文字を同じ文字にするために必要な操作回数の最小値を求めてください。 なお、そのような操作列が必ず存在することは証明可能です。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 2\times 10^5
- 2\leq N \leq 5\times 10^5
- T,N は整数
- S は
0および1からなる長さ N の文字列 - 全てのテストケースにおける N の総和は 5\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
N S
出力
T 行出力せよ。i 行目 (1\leq i\leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
3 5 01001 3 000 15 110010111100101
出力例 1
4 0 16
1 番目のテストケースについて、例えば以下のように 4 回の操作をすることで S の全ての文字を 0 にすることができます。
3 回以下の操作で S の全ての文字を同じ文字にすることはできないので、答えは 4 です。
- 先頭の文字
0を削除し、1を(削除した後の S における)1 文字目と 2 文字目の間に挿入する。S=11001になる。 - 先頭の文字
1を削除し、0を(削除した後の S における)2 文字目と 3 文字目の間に挿入する。S=10001になる。 - 末尾の文字
1を削除し、0を(削除した後の S における)末尾に挿入する。S=10000になる。 - 先頭の文字
1を削除し、0を(削除した後の S における)先頭に挿入する。S=00000になる。
2 番目のテストケースについて、はじめから S の全ての文字が同じ文字であるため、操作は 1 回も必要ありません。
Score : 400 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
You can perform the following operation on S any number of times (possibly zero):
- Delete the first or last character, flip it (change
0to1or1to0), and insert it back at any position. More formally, let r(0)=1and r(1)=0, and perform one of the following: (Here, S_i denotes the i-th character of S.)- Choose any i\ (1\leq i\leq N) and change S to S_2\dots S_i\,r(S_1)\,S_{i+1}\dots S_N.
- Choose any i\ (0\leq i\leq N-1) and change S to S_1\dots S_i\,r(S_N)\,S_{i+1}\dots S_{N-1}.
Find the minimum number of operations required to make all characters of S the same. It can be proved that such a sequence of operations always exists.
You are given T test cases, so solve each of them.
Constraints
- 1\leq T\leq 2\times 10^5
- 2\leq N \leq 5\times 10^5
- T and N are integers.
- S is a string of length N consisting of
0and1. - The sum of N over all test cases is at most 5\times 10^5.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i represents the i-th test case. Each test case is given in the following format:
N S
Output
Output T lines. The i-th line (1\leq i\leq T) should contain the answer for the i-th test case.
Sample Input 1
3 5 01001 3 000 15 110010111100101
Sample Output 1
4 0 16
For the first test case, for example, you can make all characters of S into 0 with four operations as follows.
It is impossible to make all characters of S the same with three or fewer operations, so the answer is 4.
- Delete the first character
0, and insert1between the 1st and 2nd characters (in S after deletion). S becomes11001. - Delete the first character
1, and insert0between the 2nd and 3rd characters (in S after deletion). S becomes10001. - Delete the last character
1, and insert0at the end (in S after deletion). S becomes10000. - Delete the first character
1, and insert0at the beginning (in S after deletion). S becomes00000.
For the second test case, all characters of S are the same from the beginning, so no operation is needed.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
高橋君と青木君が二次元平面上を歩きます。
高橋君のスタート地点は (TS_X, TS_Y)、ゴール地点は (TG_X, TG_Y) です。 青木君のスタート地点は (AS_X, AS_Y)、ゴール地点は (AG_X, AG_Y) です。
二人は同時に各々のスタート地点を出発し、各々のゴール地点に向かって真っ直ぐ速さ 1 で歩き続け、各々のゴール地点に到達すると停止します。 (出発は同時ですが、停止するタイミングは同時とは限らないことに注意してください。)
二人の間の距離が最も短いタイミング(二人が出発する瞬間および停止した後を含む)における、二人の間の距離を求めてください。
ここで、距離はユークリッド距離を指すものとします。すなわち、2 点 (x_1,y_1),(x_2,y_2) の間の距離は \sqrt{(x_1-x_2)^2+(y_1-y_2)^2} として定められます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 2\times 10^5
- -100\leq TS_X,TS_Y,TG_X,TG_Y,AS_X,AS_Y,AG_X,AG_Y \leq 100
- (TS_X,TS_Y)\neq (TG_X,TG_Y)
- (AS_X,AS_Y)\neq (AG_X,AG_Y)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
TS_X TS_Y TG_X TG_Y AS_X AS_Y AG_X AG_Y
出力
T 行出力せよ。i 行目 (1\leq i\leq T) には i 番目のテストケースに対する答えを出力せよ。
出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき、正解と判定される。
入力例 1
4 0 0 -2 2 -1 -1 4 4 4 0 2 0 6 0 8 0 1 0 1 1 -1 0 1 1 -8 9 2 6 -10 -10 17 20
出力例 1
1.000000000000000 2.000000000000000 0.000000000000000 1.783905950993199
1 番目のテストケースについて、二人が出発する時刻を 0 とおくと、二人の行動は以下のようになります。
- 時刻 0:高橋君が (0,0) を出発し、(-2,2) に向かって速さ 1 で歩き始める。同時に、青木君が (-1,-1) を出発し、(4,4) に向かって速さ 1 で歩き始める。
- 時刻 2\sqrt{2}:高橋君がゴール地点の (-2,2) に到達し、停止する。このとき、青木君は (1,1) におり、まだ歩き続けている。
- 時刻 5\sqrt{2}:青木君がゴール地点の (4,4) に到達し、停止する。
二人の間の距離が最も短いのは時刻 \frac{1}{\sqrt{2}} であり、このとき高橋君と青木君はそれぞれ (-\frac{1}{2},\frac{1}{2}), (-\frac{1}{2},-\frac{1}{2}) にいて、その距離は 1 です。
2 番目のテストケースについて、二人の間の距離が最も短いのは二人が出発する瞬間であり、そのときの距離は 2 です。
3 番目のテストケースについて、二人の間の距離が最も短いのは二人が共に停止した後であり、そのときの距離は 0 です。
Score : 450 points
Problem Statement
Takahashi and Aoki walk on a two-dimensional plane.
Takahashi's start point is (TS_X, TS_Y) and goal point is (TG_X, TG_Y). Aoki's start point is (AS_X, AS_Y) and goal point is (AG_X, AG_Y).
They simultaneously depart from their respective start points and walk straight toward their respective goal points at speed 1, and stop when they reach their respective goal points. (Note that they depart simultaneously, but they do not necessarily stop at the same time.)
Find the distance between them at the moment when the distance between them is shortest (including the moment they depart and after they stop).
Here, distance refers to Euclidean distance. That is, the distance between two points (x_1,y_1),(x_2,y_2) is defined as \sqrt{(x_1-x_2)^2+(y_1-y_2)^2}.
You are given T test cases, so solve each of them.
Constraints
- 1\leq T\leq 2\times 10^5
- -100\leq TS_X,TS_Y,TG_X,TG_Y,AS_X,AS_Y,AG_X,AG_Y \leq 100
- (TS_X,TS_Y)\neq (TG_X,TG_Y)
- (AS_X,AS_Y)\neq (AG_X,AG_Y)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i represents the i-th test case. Each test case is given in the following format:
TS_X TS_Y TG_X TG_Y AS_X AS_Y AG_X AG_Y
Output
Output T lines. The i-th line (1\leq i\leq T) should contain the answer for the i-th test case.
Your answer will be considered correct if the absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
4 0 0 -2 2 -1 -1 4 4 4 0 2 0 6 0 8 0 1 0 1 1 -1 0 1 1 -8 9 2 6 -10 -10 17 20
Sample Output 1
1.000000000000000 2.000000000000000 0.000000000000000 1.783905950993199
For the first test case, let time 0 be the moment they depart. Their behavior is as follows:
- Time 0: Takahashi departs from (0,0) and starts walking toward (-2,2) at speed 1. At the same time, Aoki departs from (-1,-1) and starts walking toward (4,4) at speed 1.
- Time 2\sqrt{2}: Takahashi reaches his goal point (-2,2) and stops. At this time, Aoki is at (1,1) and is still walking.
- Time 5\sqrt{2}: Aoki reaches his goal point (4,4) and stops.
The distance between them is shortest at time \frac{1}{\sqrt{2}}, when Takahashi and Aoki are at (-\frac{1}{2},\frac{1}{2}) and (-\frac{1}{2},-\frac{1}{2}) respectively, and the distance is 1.
For the second test case, the distance between them is shortest at the moment they depart, and the distance at that time is 2.
For the third test case, the distance between them is shortest after they both stop, and the distance at that time is 0.
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
AtCoder 社のオンラインショップでは現在 N 個の商品を取り扱っており、商品 i の在庫は残り A_i 個です。
以下の Q 件の注文を順に処理してください。そのうち i 件目は次の通りです。
- 商品 l_i,l_i+1,\dots,r_i を k_i 個ずつ買う。但し、 k_i 個未満の商品はあるだけ全て買う。この注文で買われた商品の個数の合計を答えよ。
i<Q について、 i 件目の注文で買われた商品の在庫を減らした状態で i+1 件目の注文に進むことに注意してください。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^{15}
- 1 \le Q \le 3 \times 10^5
- 1 \le l_i \le r_i \le N
- 1 \le k_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N Q l_1 r_1 k_1 l_2 r_2 k_2 \vdots l_Q r_Q k_Q
出力
Q 行出力せよ。
そのうち i 行目には、 i 件目の注文で買われた商品の個数の合計を答えよ。
入力例 1
6 2 6 4 5 7 5 5 1 6 1 3 5 4 4 4 1 2 5 1 1 6 100
出力例 1
6 11 0 2 10
この入力には 5 件の注文が含まれます。
- はじめ、各商品の在庫は (商品 1 から順に) 2,6,4,5,7,5 個です。
- 1 件目の注文は l_1 = 1, r_1 = 6, k_1 = 1 です。
- この注文で各商品は 1,1,1,1,1,1 個、合計 6 個買われます。
- その後、各商品の在庫は 1,5,3,4,6,4 個になります。
- 2 件目の注文は l_2 = 3, r_2 = 5, k_2 = 4 です。
- この注文で各商品は 0,0,3,4,4,0 個、合計 11 個買われます。
- その後、各商品の在庫は 1,5,0,0,2,4 個になります。
- 3 件目の注文は l_3 = 4, r_3 = 4, k_3 = 1 です。
- この注文で各商品は 0,0,0,0,0,0 個、合計 0 個買われます。
- その後、各商品の在庫は 1,5,0,0,2,4 個になります。
- 4 件目の注文は l_4 = 2, r_4 = 5, k_4 = 1 です。
- この注文で各商品は 0,1,0,0,1,0 個、合計 2 個買われます。
- その後、各商品の在庫は 1,4,0,0,1,4 個になります。
- 5 件目の注文は l_5 = 1, r_5 = 6, k_5 = 100 です。
- この注文で各商品は 1,4,0,0,1,4 個、合計 10 個買われます。
- その後、各商品の在庫は 0,0,0,0,0,0 個になります。
Score : 525 points
Problem Statement
AtCoder Inc.'s online shop currently handles N products, and the stock of product i is A_i units remaining.
Process the following Q orders in order. The i-th order is as follows:
- Buy k_i units each of products l_i,l_i+1,\dots,r_i. For products with fewer than k_i units, buy all available units. Report the total number of products bought in this order.
Note that for i<Q, the stock of products bought in the i-th order is reduced before proceeding to the (i+1)-th order.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- 1 \le A_i \le 10^{15}
- 1 \le Q \le 3 \times 10^5
- 1 \le l_i \le r_i \le N
- 1 \le k_i \le 10^9
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N Q l_1 r_1 k_1 l_2 r_2 k_2 \vdots l_Q r_Q k_Q
Output
Output Q lines.
The i-th line should contain the total number of products bought in the i-th order.
Sample Input 1
6 2 6 4 5 7 5 5 1 6 1 3 5 4 4 4 1 2 5 1 1 6 100
Sample Output 1
6 11 0 2 10
This input contains 5 orders.
- Initially, the stocks of the products are (from product 1 onward) 2,6,4,5,7,5 units.
- The first order is l_1 = 1, r_1 = 6, k_1 = 1.
- In this order, 1,1,1,1,1,1 units of the products are bought, for a total of 6 units.
- After this, the stocks of the products become 1,5,3,4,6,4 units.
- The second order is l_2 = 3, r_2 = 5, k_2 = 4.
- In this order, 0,0,3,4,4,0 units of the products are bought, for a total of 11 units.
- After this, the stocks of the products become 1,5,0,0,2,4 units.
- The third order is l_3 = 4, r_3 = 4, k_3 = 1.
- In this order, 0,0,0,0,0,0 units of the products are bought, for a total of 0 units.
- After this, the stocks of the products become 1,5,0,0,2,4 units.
- The fourth order is l_4 = 2, r_4 = 5, k_4 = 1.
- In this order, 0,1,0,0,1,0 units of the products are bought, for a total of 2 units.
- After this, the stocks of the products become 1,4,0,0,1,4 units.
- The fifth order is l_5 = 1, r_5 = 6, k_5 = 100.
- In this order, 1,4,0,0,1,4 units of the products are bought, for a total of 10 units.
- After this, the stocks of the products become 0,0,0,0,0,0 units.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
1 から N までの番号が付けられた N 個のアイテムがあります。 アイテム i の 重み は W_i、価値 は V_i です。
Q 個のクエリが与えられるので、順に処理してください。 j 番目のクエリは以下の通りです。
- 整数 L_j, R_j, C_j\ (1\leq L_j\leq R_j\leq N) が与えられる。 アイテム L_j,L_j+1,\dots,R_j のうちいくつか(0 個でもよい)を、重みの総和が C_j を超えないように選ぶとき、選んだアイテムの価値の総和が最大でいくつになるか求めよ。
制約
- 1\leq N \leq 2\times 10^4
- 1\leq Q \leq 2\times 10^5
- 1\leq W_i \leq 500
- 1\leq V_i \leq 10^9
- 1\leq L_j \leq R_j \leq N
- 1\leq C_j \leq 500
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N W_1 V_1 W_2 V_2 \vdots W_N V_N Q L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_Q R_Q C_Q
出力
Q 行出力せよ。 i 行目 (1\leq i \leq Q) には、i 番目のクエリに対する答えを出力せよ。
入力例 1
4 3 4 5 8 1 2 2 3 3 1 4 7 2 4 10 1 2 2
出力例 1
11 13 0
1 番目のクエリについて、アイテム 1,2,3,4 のうちアイテム 2,4 を選ぶと、重みの総和は 5+2=7 となり C_1=7 を超えず、価値の総和として 8+3=11 を達成できます。これが最大です。
2 番目のクエリについて、アイテム 2,3,4 全てを選んでも、重みの総和は 5+1+2=8 となり C_2=10 を超えず、価値の総和として 8+2+3=13 を達成できます。
3 番目のクエリについて、アイテム 1,2 のいずれも重みが C_3=2 を超えているため、一つもアイテムを選ぶことができず、価値の総和の最大値は 0 になります。
入力例 2
8 167 430302156 22 623690081 197 476190629 176 24979445 22 877914575 247 211047202 232 822804784 25 628894325 8 6 8 176 3 5 80 1 7 310 4 8 368 4 5 218 3 4 431 4 6 228 1 1 239
出力例 2
628894325 877914575 2324409440 2329613684 902894020 501170074 902894020 430302156
Score : 575 points
Problem Statement
There are N items numbered from 1 to N. The weight of item i is W_i and the value is V_i.
You are given Q queries, so process them in order. The j-th query is as follows:
- Integers L_j, R_j, C_j\ (1\leq L_j\leq R_j\leq N) are given. When choosing some (possibly zero) items from items L_j,L_j+1,\dots,R_j such that the total weight does not exceed C_j, find the maximum possible total value of the chosen items.
Constraints
- 1\leq N \leq 2\times 10^4
- 1\leq Q \leq 2\times 10^5
- 1\leq W_i \leq 500
- 1\leq V_i \leq 10^9
- 1\leq L_j \leq R_j \leq N
- 1\leq C_j \leq 500
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N W_1 V_1 W_2 V_2 \vdots W_N V_N Q L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_Q R_Q C_Q
Output
Output Q lines. The i-th line (1\leq i \leq Q) should contain the answer for the i-th query.
Sample Input 1
4 3 4 5 8 1 2 2 3 3 1 4 7 2 4 10 1 2 2
Sample Output 1
11 13 0
For the first query, among items 1,2,3,4, if you choose items 2,4, the total weight is 5+2=7, which does not exceed C_1=7, and the total value is 8+3=11. This is the maximum.
For the second query, even if you choose all items 2,3,4, the total weight is 5+1+2=8, which does not exceed C_2=10, and you can achieve a total value of 8+2+3=13.
For the third query, both items 1,2 have weights exceeding C_3=2, so you cannot choose any item, and the maximum total value is 0.
Sample Input 2
8 167 430302156 22 623690081 197 476190629 176 24979445 22 877914575 247 211047202 232 822804784 25 628894325 8 6 8 176 3 5 80 1 7 310 4 8 368 4 5 218 3 4 431 4 6 228 1 1 239
Sample Output 2
628894325 877914575 2324409440 2329613684 902894020 501170074 902894020 430302156