実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 233 点
問題文
高橋君を含む N 人のグループが遊園地にやってきました。この遊園地には M 個のアトラクションがあります。
グループのメンバーにはそれぞれ身長があり、メンバー j (1 \leq j \leq N) の身長は L_j cm です。また、各アトラクションには身長制限が設けられており、アトラクション i (1 \leq i \leq M) に乗るには身長が S_i cm 以上でなければなりません。すなわち、メンバー j がアトラクション i に乗れるのは L_j \geq S_i を満たすときに限ります。
高橋君は、グループの N 人全員が身長制限を満たして乗ることのできるアトラクションがいくつあるか知りたいと思っています。すなわち、すべてのメンバー j (1 \leq j \leq N) について L_j \geq S_i が成り立つようなアトラクション i の個数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq L_j \leq 300 (1 \leq j \leq N)
- 1 \leq S_i \leq 300 (1 \leq i \leq M)
- 入力はすべて整数
入力
N M L_1 L_2 \ldots L_N S_1 S_2 \ldots S_M
- 1 行目には、グループの人数を表す整数 N と、アトラクションの数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各メンバーの身長(cm)を表す整数 L_1, L_2, \ldots, L_N が、スペース区切りで与えられる。
- 3 行目には、各アトラクションの身長制限(cm)を表す整数 S_1, S_2, \ldots, S_M が、スペース区切りで与えられる。
出力
グループの N 人全員が乗ることのできるアトラクションの数を 1 行で出力してください。
入力例 1
3 4 150 140 160 130 150 145 170
出力例 1
1
入力例 2
5 6 120 135 128 140 132 110 125 130 140 135 150
出力例 2
1
入力例 3
10 8 165 172 158 180 169 175 162 170 168 155 150 160 155 170 165 180 145 175
出力例 3
3
Score : 233 pts
Problem Statement
A group of N people, including Takahashi, has come to an amusement park. This amusement park has M attractions.
Each member of the group has a certain height. The height of member j (1 \leq j \leq N) is L_j cm. Additionally, each attraction has a height restriction: to ride attraction i (1 \leq i \leq M), one must have a height of at least S_i cm. That is, member j can ride attraction i only when L_j \geq S_i is satisfied.
Takahashi wants to know how many attractions all N members of the group can ride while satisfying the height restrictions. In other words, find the number of attractions i such that L_j \geq S_i holds for every member j (1 \leq j \leq N).
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq L_j \leq 300 (1 \leq j \leq N)
- 1 \leq S_i \leq 300 (1 \leq i \leq M)
- All inputs are integers.
Input
N M L_1 L_2 \ldots L_N S_1 S_2 \ldots S_M
- The first line contains an integer N representing the number of people in the group and an integer M representing the number of attractions, separated by a space.
- The second line contains integers L_1, L_2, \ldots, L_N representing the height (in cm) of each member, separated by spaces.
- The third line contains integers S_1, S_2, \ldots, S_M representing the height restriction (in cm) of each attraction, separated by spaces.
Output
Print in one line the number of attractions that all N members of the group can ride.
Sample Input 1
3 4 150 140 160 130 150 145 170
Sample Output 1
1
Sample Input 2
5 6 120 135 128 140 132 110 125 130 140 135 150
Sample Output 2
1
Sample Input 3
10 8 165 172 158 180 169 175 162 170 168 155 150 160 155 170 165 180 145 175
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は、とある名門オーケストラの入団オーディションに参加しています。オーディションには N 人の候補者が参加しており、それぞれ 1 から N の番号が付けられています。高橋君の番号は 1 です。
オーディションでは、各候補者に実技の得点が付けられます。候補者 i (1 \leq i \leq N) の得点は S_i 点です。
ところが、結果発表の直前になって、M 人の候補者が辞退届を提出しました。辞退した候補者の番号は D_1, D_2, \ldots, D_M です。高橋君は辞退しません(すなわち、D_j \neq 1 が保証されます)。
辞退した候補者を除外した残りの候補者は、得点が高い順に順位付けされます。得点が同じ候補者が複数いる場合は、番号が小さい候補者ほど上位とします。このルールにより、残りの候補者全員に一意な順位が定まります。この順位付けにおいて上位 K 人が合格となります。
なお、辞退者を除外した後の候補者の人数は K 人以上であることが保証されます。
高橋君が合格できるかどうかを判定してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 0 \leq M \leq N - 1
- K \leq N - M
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_j \leq N (1 \leq j \leq M)
- D_j \neq 1 (1 \leq j \leq M)
- D_1, D_2, \ldots, D_M はすべて異なる
- 入力はすべて整数
入力
N K M S_1 S_2 \ldots S_N D_1 D_2 \ldots D_M
- 1 行目には、候補者の人数 N 、合格人数 K 、辞退者の人数 M が、スペース区切りで与えられる。
- 2 行目には、候補者 1, 2, \ldots, N の得点 S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。
- 3 行目には、辞退した候補者の番号 D_1, D_2, \ldots, D_M が、スペース区切りで与えられる。ただし、M = 0 の場合、3 行目は与えられない(入力は 2 行のみからなる)。
出力
高橋君(候補者 1 )が合格できる場合は Yes を、できない場合は No を 1 行で出力せよ。
入力例 1
5 2 1 80 100 90 70 60 2
出力例 1
Yes
入力例 2
7 3 0 85 90 85 85 70 60 50
出力例 2
Yes
入力例 3
10 2 3 30 95 88 76 100 60 92 55 40 85 4 6 8
出力例 3
No
Score : 300 pts
Problem Statement
Takahashi is participating in an audition for a prestigious orchestra. There are N candidates participating in the audition, each numbered from 1 to N. Takahashi's number is 1.
In the audition, each candidate is given a practical skill score. The score of candidate i (1 \leq i \leq N) is S_i points.
However, just before the results are announced, M candidates submitted withdrawal notices. The numbers of the candidates who withdrew are D_1, D_2, \ldots, D_M. Takahashi does not withdraw (that is, it is guaranteed that D_j \neq 1).
The remaining candidates, after excluding those who withdrew, are ranked in descending order of score. If multiple candidates have the same score, the candidate with the smaller number is ranked higher. Under this rule, a unique rank is determined for all remaining candidates. The top K candidates in this ranking pass the audition.
It is guaranteed that the number of candidates after excluding those who withdrew is at least K.
Determine whether Takahashi can pass the audition.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq N
- 0 \leq M \leq N - 1
- K \leq N - M
- 0 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_j \leq N (1 \leq j \leq M)
- D_j \neq 1 (1 \leq j \leq M)
- D_1, D_2, \ldots, D_M are all distinct
- All input values are integers
Input
N K M S_1 S_2 \ldots S_N D_1 D_2 \ldots D_M
- The first line contains the number of candidates N, the number of candidates who pass K, and the number of candidates who withdrew M, separated by spaces.
- The second line contains the scores S_1, S_2, \ldots, S_N of candidates 1, 2, \ldots, N, separated by spaces.
- The third line contains the numbers D_1, D_2, \ldots, D_M of the candidates who withdrew, separated by spaces. However, if M = 0, the third line is not given (the input consists of only 2 lines).
Output
If Takahashi (candidate 1) can pass the audition, print Yes; otherwise, print No on a single line.
Sample Input 1
5 2 1 80 100 90 70 60 2
Sample Output 1
Yes
Sample Input 2
7 3 0 85 90 85 85 70 60 50
Sample Output 2
Yes
Sample Input 3
10 2 3 30 95 88 76 100 60 92 55 40 85 4 6 8
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は工場を経営しており、N 種類の製品を製造・販売することで利益を得ています。
工場には M 台の製造機械があります。各機械の稼働可能時間はいずれも K 分間であり、各機械で製造する製品の製造時間の合計は K 分以下でなければなりません。
各製品 i(1 \leq i \leq N)には以下の情報が与えられます:
- C_i:製品 i を 1 つ製造するための原材料コスト(円)
- T_i:製品 i を 1 つ製造するのに必要な時間(分)
- P_i:製品 i を 1 つ販売したときの売値(円)
製品 i を 1 つ製造して販売すると、P_i - C_i 円の利益が得られます(この値が負の場合は損失となります)。
各機械は一度に 1 つの製品しか製造できず、製品の製造を途中で中断することはできません。1 つの製品の製造が完了したら、切り替え時間なしですぐに次の製品の製造を開始できます。
M 台の機械はそれぞれ独立に動作します。原材料は十分にあり、どの種類の製品も個数の上限なく製造できます。各機械で製造する製品の種類と個数は機械ごとに自由に決めることができ、同じ種類の製品を複数の機械で製造することもできます。
また、ある機械で製品を 1 つも製造しないことも許されます。製品を 1 つも製造しなかった機械からの利益は 0 円です。
M 台すべての機械から得られる利益の合計の最大値を求めてください。
制約
- 1 \leq N \leq 500
- 1 \leq M \leq 10^9
- 1 \leq K \leq 200{,}000
- 1 \leq C_i \leq 10^6(1 \leq i \leq N)
- 1 \leq T_i \leq K(1 \leq i \leq N)
- 1 \leq P_i \leq 10^6(1 \leq i \leq N)
- 入力はすべて整数である。
- 答えは 2^{63} - 1(= 9{,}223{,}372{,}036{,}854{,}775{,}807、64 ビット符号付き整数の最大値)以下であることが保証される。
入力
N M K C_1 T_1 P_1 C_2 T_2 P_2 \vdots C_N T_N P_N
- 1 行目には、製品の種類数 N、機械の台数 M、各機械の稼働可能時間(分)K が、空白区切りで与えられる。
- 続く N 行のうち i 行目(1 \leq i \leq N)には、製品 i の原材料コスト C_i、製造に必要な時間 T_i、売値 P_i が、空白区切りで与えられる。
出力
得られる利益の合計の最大値を 1 行で出力せよ。
入力例 1
3 2 10 3 4 10 2 3 8 5 6 12
出力例 1
38
入力例 2
2 3 5 10 2 8 7 5 6
出力例 2
0
入力例 3
8 100 1000 120 50 260 200 80 390 90 30 150 300 120 500 500 200 850 40 25 70 1000 500 1600 250 70 410
出力例 3
280000
入力例 4
25 123456789 200000 100000 1 100500 200000 7 203000 300000 13 306500 400000 29 414000 500000 100 560000 600000 256 720000 700000 999 1000000 150000 1234 260000 250000 4096 450000 350000 8192 690000 450000 15000 900000 550000 20000 950000 650000 30000 980000 750000 50000 990000 800000 75000 1000000 900000 100000 1000000 100000 11111 180000 200000 22222 350000 300000 33333 520000 400000 44444 710000 500000 55555 830000 600000 66666 920000 700000 77777 970000 800000 88888 990000 900000 199999 1000000
出力例 4
14814814680000000
入力例 5
1 1 1 1 1 1
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi runs a factory and earns profit by manufacturing and selling N types of products.
The factory has M manufacturing machines. Each machine has an available operating time of K minutes, and the total manufacturing time of products produced on each machine must not exceed K minutes.
For each product i (1 \leq i \leq N), the following information is given:
- C_i: The raw material cost to manufacture one unit of product i (yen)
- T_i: The time required to manufacture one unit of product i (minutes)
- P_i: The selling price of one unit of product i (yen)
Manufacturing and selling one unit of product i yields a profit of P_i - C_i yen (if this value is negative, it represents a loss).
Each machine can only manufacture one product at a time, and manufacturing of a product cannot be interrupted midway. Once the manufacturing of one product is completed, the next product can be started immediately without any switching time.
The M machines operate independently of each other. There are sufficient raw materials, and any type of product can be manufactured without an upper limit on quantity. The types and quantities of products manufactured on each machine can be freely chosen for each machine, and the same type of product can be manufactured on multiple machines.
It is also permitted for a machine to manufacture no products at all. The profit from a machine that manufactures no products is 0 yen.
Find the maximum total profit that can be obtained from all M machines.
Constraints
- 1 \leq N \leq 500
- 1 \leq M \leq 10^9
- 1 \leq K \leq 200{,}000
- 1 \leq C_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq T_i \leq K (1 \leq i \leq N)
- 1 \leq P_i \leq 10^6 (1 \leq i \leq N)
- All inputs are integers.
- It is guaranteed that the answer does not exceed 2^{63} - 1 (= 9{,}223{,}372{,}036{,}854{,}775{,}807, the maximum value of a 64-bit signed integer).
Input
N M K C_1 T_1 P_1 C_2 T_2 P_2 \vdots C_N T_N P_N
- The first line contains the number of product types N, the number of machines M, and the available operating time per machine (in minutes) K, separated by spaces.
- In the following N lines, the i-th line (1 \leq i \leq N) contains the raw material cost C_i, the manufacturing time T_i, and the selling price P_i of product i, separated by spaces.
Output
Output the maximum total profit that can be obtained in a single line.
Sample Input 1
3 2 10 3 4 10 2 3 8 5 6 12
Sample Output 1
38
Sample Input 2
2 3 5 10 2 8 7 5 6
Sample Output 2
0
Sample Input 3
8 100 1000 120 50 260 200 80 390 90 30 150 300 120 500 500 200 850 40 25 70 1000 500 1600 250 70 410
Sample Output 3
280000
Sample Input 4
25 123456789 200000 100000 1 100500 200000 7 203000 300000 13 306500 400000 29 414000 500000 100 560000 600000 256 720000 700000 999 1000000 150000 1234 260000 250000 4096 450000 350000 8192 690000 450000 15000 900000 550000 20000 950000 650000 30000 980000 750000 50000 990000 800000 75000 1000000 900000 100000 1000000 100000 11111 180000 200000 22222 350000 300000 33333 520000 400000 44444 710000 500000 55555 830000 600000 66666 920000 700000 77777 970000 800000 88888 990000 900000 199999 1000000
Sample Output 4
14814814680000000
Sample Input 5
1 1 1 1 1 1
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は花火大会の演出を担当しています。夜空には N 発の花火が打ち上げ準備されており、それぞれ 1 から N までの番号が付いています。
各花火には、打ち上げ準備中・打ち上げ済み・消滅 の 3 つの状態があります。最初、すべての花火は打ち上げ準備中の状態です。
各花火 i(1 \leq i \leq N)には「衝撃波の対象」となる花火番号 T_i が設定されています(T_i \neq i)。
青木君は演出家として、花火を一発ずつ選んで打ち上げる順番を決めます。具体的には、以下の手順で花火大会が進みます。
- 青木君は打ち上げ準備中の花火の中から 1 発を選び、打ち上げる。打ち上げた花火の状態は打ち上げ済みとなり、演出リストの末尾に記録される。
- 打ち上げた花火を i とする。花火 i の衝撃波の対象である花火 T_i がまだ打ち上げ準備中であれば、花火 T_i は暴発し、状態が消滅に変わる(演出リストには記録されない)。花火 T_i がすでに打ち上げ済みまたは消滅であれば、何も起こらない。
- 打ち上げ準備中の花火が 1 発以上残っていれば手順 1 に戻る。打ち上げ準備中の花火がなくなったら花火大会は終了する。
注意: 消滅した花火は打ち上げることができません。また、消滅した花火が衝撃波を発することはなく、消滅によって連鎖的に他の花火が暴発することはありません。
花火大会が終了すると、演出リストが確定します。青木君は、演出リストに記録される花火の数(すなわち、打ち上げ済みとなった花火の数)をできるだけ多くしたいと考えています。
青木君が最適に花火を選んだとき、演出リストに記録される花火の数の最大値を求めてください。
制約
- 2 \leq N \leq 10^6
- 1 \leq T_i \leq N(1 \leq i \leq N)
- T_i \neq i(1 \leq i \leq N)
- 入力はすべて整数である。
入力
N T_1 T_2 \cdots T_N
- 1 行目には、花火の数 N が与えられる。
- 2 行目には、各花火 i(1 \leq i \leq N)の衝撃波の対象を表す花火番号 T_i が、スペース区切りで N 個与えられる。
出力
青木君が最適に行動したときの、演出リストに記録される花火の数の最大値を 1 行で出力せよ。
入力例 1
4 2 1 4 3
出力例 1
2
入力例 2
4 2 3 4 1
出力例 2
3
入力例 3
10 2 3 1 5 6 4 1 3 6 7
出力例 3
8
入力例 4
20 2 3 1 5 6 7 8 4 10 11 12 9 14 15 13 1 2 3 9 13
出力例 4
16
入力例 5
2 2 1
出力例 5
1
Score : 400 pts
Problem Statement
Takahashi is in charge of directing a fireworks show. In the night sky, N fireworks are prepared for launch, numbered from 1 to N.
Each firework has one of three states: ready, launched, or destroyed. Initially, all fireworks are in the ready state.
For each firework i (1 \leq i \leq N), a "shockwave target" firework number T_i is assigned (T_i \neq i).
Aoki, as the director, decides the order in which to launch the fireworks one by one. Specifically, the fireworks show proceeds as follows:
- Aoki selects one firework from among the ready fireworks and launches it. The launched firework's state becomes launched, and it is recorded at the end of the performance list.
- Let i be the launched firework. If firework T_i, the shockwave target of firework i, is still in the ready state, then firework T_i detonates prematurely and its state changes to destroyed (it is not recorded in the performance list). If firework T_i is already launched or destroyed, nothing happens.
- If one or more ready fireworks remain, return to step 1. If no ready fireworks remain, the fireworks show ends.
Note: Destroyed fireworks cannot be launched. Additionally, destroyed fireworks do not emit shockwaves, and destruction does not cause chain detonations of other fireworks.
When the fireworks show ends, the performance list is finalized. Aoki wants to maximize the number of fireworks recorded in the performance list (that is, the number of fireworks that were launched).
Determine the maximum number of fireworks recorded in the performance list when Aoki chooses optimally.
Constraints
- 2 \leq N \leq 10^6
- 1 \leq T_i \leq N (1 \leq i \leq N)
- T_i \neq i (1 \leq i \leq N)
- All input values are integers.
Input
N T_1 T_2 \cdots T_N
- The first line gives the number of fireworks N.
- The second line gives N space-separated integers, where T_i represents the shockwave target firework number for each firework i (1 \leq i \leq N).
Output
Output in one line the maximum number of fireworks recorded in the performance list when Aoki acts optimally.
Sample Input 1
4 2 1 4 3
Sample Output 1
2
Sample Input 2
4 2 3 4 1
Sample Output 2
3
Sample Input 3
10 2 3 1 5 6 4 1 3 6 7
Sample Output 3
8
Sample Input 4
20 2 3 1 5 6 7 8 4 10 11 12 9 14 15 13 1 2 3 9 13
Sample Output 4
16
Sample Input 5
2 2 1
Sample Output 5
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
N 人の選手が一列に並んでおり、左から順に 1, 2, \ldots, N の番号が付いています。
各選手 i(1 \leq i \leq N)は、以下の情報を持っています:
- キャッチ力 D_i:この値以下のスピードのボールをキャッチできます。
- パス範囲 [L_i, R_i]:L_i \leq i \leq R_i を満たす区間で、ボールをパスできる相手の範囲を表します。この区間には選手 i 自身の位置が必ず含まれます。
高橋君はこの選手たちに M 個のボールを投げ入れます。各ボール j(1 \leq j \leq M)は初期スピード W_j を持ち、最初に選手 T_j に渡されます。M 個のボールはそれぞれ独立に処理され、あるボールの結果が他のボールに影響することはありません。
ボールを受け取った選手は、最初にボールを受け取った選手も含めて、以下のルールに従って行動します:
選手 i がスピード W のボールを受け取ったとします(パスを繰り返す過程で W が 0 になることもあります)。
- W \leq D_i の場合(キャッチ): 選手 i はそのボールをキャッチします。ボールの移動はここで終了します。
- W > D_i の場合(パス): 選手 i はそのボールをキャッチできず、パスを試みます。
- L_i = R_i = i の場合(パス範囲内に自分以外の選手がいない場合):パスを行えず、選手 i がそのボールの最終保持者となります。ボールの移動はここで終了します。
- それ以外の場合:選手 i のパス範囲 [L_i, R_i] に含まれる選手のうち、選手 i 自身を除いて最も番号の小さい選手にボールを渡します。このとき、ボールのスピードは 1 減少し W - 1 となります。パス先は選手 i より左(番号が小さい方)になることも右(番号が大きい方)になることもあります。
パスが左方向にも行われうるため、同じ選手を複数回訪れることがあります。ただし、パスのたびにスピードは 1 減少し、すべての選手について D_i \geq 0 であるため、各ボールについてボールの移動は必ず有限回で終了します。
M 個のボールそれぞれについて、最終的な結果を出力してください。ボールがキャッチされた場合(上記ルール 1 で終了した場合)は -1 を、最終保持者が存在する場合(上記ルール 2 の前者で終了した場合)はその選手の番号を出力してください。
制約
- 1 \leq N, M \leq 2 \times 10^5
- N + M \leq 2 \times 10^5
- 0 \leq D_i \leq 10^9
- 1 \leq L_i \leq i \leq R_i \leq N
- 1 \leq T_j \leq N
- 1 \leq W_j \leq 10^9
- 入力はすべて整数である。
入力
N M D_1 L_1 R_1 D_2 L_2 R_2 \vdots D_N L_N R_N T_1 W_1 T_2 W_2 \vdots T_M W_M
- 1 行目には、選手の人数を表す整数 N と、ボールの個数を表す整数 M が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各選手の情報が与えられる。
- 1 + i 行目(1 \leq i \leq N)では、選手 i のキャッチ力 D_i 、パス範囲の左端 L_i 、パス範囲の右端 R_i が、スペース区切りで与えられる。L_i \leq i \leq R_i が成り立つ。
- N + 2 行目から N + M + 1 行目では、各ボールの情報が与えられる。
- N + 1 + j 行目(1 \leq j \leq M)では、ボール j の最初の受け取り手の選手番号 T_j と、ボール j の初期スピード W_j が、スペース区切りで与えられる。
出力
M 行出力せよ。j 行目(1 \leq j \leq M)には、ボール j がキャッチされた場合は -1 を、最終保持者が存在する場合はその選手の番号を出力せよ。
入力例 1
4 5 2 1 2 0 1 3 5 3 4 1 4 4 1 1 1 4 2 3 3 7 4 2
出力例 1
-1 -1 -1 4 4
入力例 2
3 4 10 1 1 1 1 2 0 2 3 1 10 2 2 3 2 3 1
出力例 2
-1 -1 -1 -1
入力例 3
8 10 0 1 1 3 1 2 1 2 5 4 4 6 2 3 5 0 5 8 10 7 8 1 6 8 1 5 2 4 3 2 4 6 5 5 6 3 7 11 8 2 4 4 6 1
出力例 3
1 1 -1 -1 -1 -1 1 -1 -1 -1
入力例 4
15 15 5 1 3 2 1 4 0 2 5 7 4 4 1 3 7 4 6 9 0 5 8 10 7 10 3 1 9 6 10 12 0 9 13 2 12 15 8 11 14 1 13 15 0 15 15 1 6 2 5 3 1 4 1000000000 5 9 6 6 7 2 8 11 9 4 10 20 11 3 12 5 13 9 14 2 15 1
出力例 4
-1 -1 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 15
入力例 5
1 2 0 1 1 1 1 1 1000000000
出力例 5
1 1
Score : 466 pts
Problem Statement
N players are standing in a row, numbered 1, 2, \ldots, N from left to right.
Each player i (1 \leq i \leq N) has the following information:
- Catching Power D_i: They can catch a ball with a speed less than or equal to this value.
- Passing Range [L_i, R_i]: An interval satisfying L_i \leq i \leq R_i that represents the range of players to whom they can pass the ball. This interval always contains player i's own position.
Takahashi will throw M balls to these players. Each ball j (1 \leq j \leq M) has an initial speed W_j and is initially handed to player T_j. The M balls are processed independently, and the result of one ball does not affect any other balls.
A player who receives a ball (including the player who receives it initially) acts according to the following rules:
Suppose player i receives a ball with speed W (note that W may become 0 through repeated passes).
- If W \leq D_i (Catch): Player i catches the ball. The movement of the ball ends here.
- If W > D_i (Pass): Player i cannot catch the ball and attempts to pass it.
- If L_i = R_i = i (i.e., there is no player other than themselves in their passing range): They cannot make a pass, and player i becomes the final holder of the ball. The movement of the ball ends here.
- Otherwise: They pass the ball to the player with the smallest index other than player i themselves among the players in player i's passing range [L_i, R_i]. At this time, the speed of the ball decreases by 1 to become W - 1. The receiver of the pass can be either to the left (smaller index) or to the right (larger index) of player i.
Since passes can also be made to the left, the same player may be visited multiple times. However, since the speed decreases by 1 with each pass and D_i \geq 0 holds for all players, the movement of each ball is guaranteed to end after a finite number of steps.
For each of the M balls, output the final result. If the ball is caught (ends by rule 1 above), output -1. If there is a final holder (ends by the former case of rule 2 above), output the index of that player.
Constraints
- 1 \leq N, M \leq 2 \times 10^5
- N + M \leq 2 \times 10^5
- 0 \leq D_i \leq 10^9
- 1 \leq L_i \leq i \leq R_i \leq N
- 1 \leq T_j \leq N
- 1 \leq W_j \leq 10^9
- All input values are integers.
Input
N M D_1 L_1 R_1 D_2 L_2 R_2 \vdots D_N L_N R_N T_1 W_1 T_2 W_2 \vdots T_M W_M
- The first line contains an integer N, representing the number of players, and an integer M, representing the number of balls, separated by a space.
- The next N lines, from the 2nd to the (N + 1)-th line, contain the information of each player.
- The (1 + i)-th line (1 \leq i \leq N) contains the catching power D_i, the left end of the passing range L_i, and the right end of the passing range R_i of player i, separated by spaces. It is guaranteed that L_i \leq i \leq R_i.
- The following M lines, from the (N + 2)-th to the (N + M + 1)-th line, contain the information of each ball.
- The (N + 1 + j)-th line (1 \leq j \leq M) contains the initial receiver's player index T_j and the initial speed W_j of ball j, separated by a space.
Output
Print M lines. The j-th line (1 \leq j \leq M) should contain -1 if ball j is caught, and the index of the final holder if there is a final holder.
Sample Input 1
4 5 2 1 2 0 1 3 5 3 4 1 4 4 1 1 1 4 2 3 3 7 4 2
Sample Output 1
-1 -1 -1 4 4
Sample Input 2
3 4 10 1 1 1 1 2 0 2 3 1 10 2 2 3 2 3 1
Sample Output 2
-1 -1 -1 -1
Sample Input 3
8 10 0 1 1 3 1 2 1 2 5 4 4 6 2 3 5 0 5 8 10 7 8 1 6 8 1 5 2 4 3 2 4 6 5 5 6 3 7 11 8 2 4 4 6 1
Sample Output 3
1 1 -1 -1 -1 -1 1 -1 -1 -1
Sample Input 4
15 15 5 1 3 2 1 4 0 2 5 7 4 4 1 3 7 4 6 9 0 5 8 10 7 10 3 1 9 6 10 12 0 9 13 2 12 15 8 11 14 1 13 15 0 15 15 1 6 2 5 3 1 4 1000000000 5 9 6 6 7 2 8 11 9 4 10 20 11 3 12 5 13 9 14 2 15 1
Sample Output 4
-1 -1 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 15
Sample Input 5
1 2 0 1 1 1 1 1 1000000000
Sample Output 5
1 1