実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君はフリーマーケットに出店しています。彼のブースでは N 種類の商品を販売しており、 i 番目の商品 (1 \leq i \leq N) の価格は P_i 円です。各商品の在庫は十分にあるものとします。
イベント中、 M 人のお客さんが高橋君のブースで買い物をしました。 j 番目のお客さん (1 \leq j \leq M) は、 T_j 番目の商品を Q_j 個購入しました。
高橋君は最初 S 円の現金を持っています。お客さんとの取引は 1 番目のお客さんから順番に 1 人ずつ処理されます。 j 番目のお客さんとの取引では、以下の手順がこの順に行われます。
- 売上金額 X = P_{T_j} \times Q_j 円を計算する。
- お客さんからの支払いとして、高橋君の手元の現金に X 円が加算される。
- イベント運営への販売手数料として、手元の現金から \lfloor X / 2 \rfloor 円が差し引かれる。ここで \lfloor a \rfloor は実数 a 以下の最大の整数を表す。
各取引において、手数料 \lfloor X / 2 \rfloor は売上金額 X 以下であるため、取引による現金の変化量は 0 以上です。したがって、取引の過程で手元の現金が負になることはありません。
M 人すべてのお客さんとの取引が完了した後、高橋君の手元にある現金は何円になっているでしょうか?
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 0 \leq S \leq 10^9
- 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
- 1 \leq T_j \leq N (1 \leq j \leq M)
- 1 \leq Q_j \leq 10^4 (1 \leq j \leq M)
- 入力はすべて整数
- 答えは 0 以上 10^{13} 以下になることが保証される
入力
N M S P_1 P_2 \ldots P_N T_1 Q_1 T_2 Q_2 \vdots T_M Q_M
- 1 行目には、商品の種類数を表す N 、お客さんの人数を表す M 、初期の所持金を表す S が、スペース区切りで与えられる。
- 2 行目には、各商品の価格 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
- 続く M 行のうち j 行目 (1 \leq j \leq M) には、 j 番目のお客さんが購入した商品の種類 T_j と購入個数 Q_j が、スペース区切りで与えられる。
出力
すべての取引が完了した後の高橋君の所持金を 1 行で出力せよ。
入力例 1
3 2 100 200 500 300 1 2 3 1
出力例 1
450
入力例 2
5 4 1000 150 300 99 500 1200 3 3 5 1 1 5 2 2
出力例 2
2424
入力例 3
6 5 0 1 9999 5000 3 7777 10000 2 10000 4 1 6 9999 1 1 5 5000
出力例 3
119432503
Score : 266 pts
Problem Statement
Takahashi is running a booth at a flea market. His booth sells N types of products, and the price of the i-th product (1 \leq i \leq N) is P_i yen. Assume that the stock of each product is sufficient.
During the event, M customers made purchases at Takahashi's booth. The j-th customer (1 \leq j \leq M) purchased Q_j units of the T_j-th product.
Takahashi initially has S yen in cash. Transactions with customers are processed one by one in order, starting from the 1st customer. The transaction with the j-th customer proceeds as follows, in this order:
- Calculate the sales amount X = P_{T_j} \times Q_j yen.
- As payment from the customer, X yen is added to Takahashi's cash on hand.
- As a sales commission to the event organizer, \lfloor X / 2 \rfloor yen is deducted from his cash on hand. Here, \lfloor a \rfloor denotes the largest integer not exceeding the real number a.
In each transaction, the commission \lfloor X / 2 \rfloor is at most the sales amount X, so the net change in cash from a transaction is non-negative. Therefore, the cash on hand never becomes negative during the process of transactions.
After all transactions with the M customers have been completed, how many yen does Takahashi have on hand?
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 0 \leq S \leq 10^9
- 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
- 1 \leq T_j \leq N (1 \leq j \leq M)
- 1 \leq Q_j \leq 10^4 (1 \leq j \leq M)
- All input values are integers
- It is guaranteed that the answer is between 0 and 10^{13}, inclusive
Input
N M S P_1 P_2 \ldots P_N T_1 Q_1 T_2 Q_2 \vdots T_M Q_M
- The first line contains N representing the number of product types, M representing the number of customers, and S representing the initial amount of cash, separated by spaces.
- The second line contains the prices of each product P_1, P_2, \ldots, P_N, separated by spaces.
- In the following M lines, the j-th line (1 \leq j \leq M) contains the product type T_j purchased by the j-th customer and the quantity Q_j, separated by a space.
Output
Print on a single line the amount of cash Takahashi has after all transactions have been completed.
Sample Input 1
3 2 100 200 500 300 1 2 3 1
Sample Output 1
450
Sample Input 2
5 4 1000 150 300 99 500 1200 3 3 5 1 1 5 2 2
Sample Output 2
2424
Sample Input 3
6 5 0 1 9999 5000 3 7777 10000 2 10000 4 1 6 9999 1 1 5 5000
Sample Output 3
119432503
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は M 人の子どもたちにお菓子を配ることになりました。子どもたちにはそれぞれ 1 から M までの番号が付けられています。お菓子は全部で S 個あります。
各子ども i(1 \leq i \leq M)は、分配の前からすでに B_i 個のお菓子を持っています。
分配のルールは以下の通りです:
- S を M で割った商を q = \lfloor S / M \rfloor、余りを r = S \mod M とする。
- すべての子どもに q 個ずつ配る。
- さらに、余った r 個のお菓子を、番号の小さい子どもから順に 1 個ずつ追加で配る。すなわち、子ども 1, 2, \ldots, r にそれぞれ追加で 1 個ずつ配る(r = 0 の場合、追加の配布は行わない)。
以上により、子ども i が今回の分配でもらうお菓子の個数は、i \leq r ならば q + 1 個、i > r ならば q 個です。
子ども i の 最終的なお菓子の個数 を、もともと持っていた B_i 個と今回もらった個数の合計と定めます。
高橋君は、青木君からの N 個の質問に答えなければなりません。j 番目の質問(1 \leq j \leq N)では、子ども L_j から子ども R_j まで(両端を含む)の最終的なお菓子の個数の合計を求めてください。
N 個の質問それぞれに対して、答えを出力してください。
制約
- 1 \leq M \leq 2 \times 10^5
- 0 \leq S \leq 10^{9}
- 0 \leq B_i \leq 10^{9}(1 \leq i \leq M)
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_j \leq R_j \leq M(1 \leq j \leq N)
- 入力はすべて整数である。
入力
M S B_1 B_2 \ldots B_M N L_1 R_1 L_2 R_2 \vdots L_N R_N
- 1 行目には、子どもの人数 M と、お菓子の総数 S がスペース区切りで与えられる。
- 2 行目には、各子どもがもともと持っているお菓子の個数 B_1, B_2, \ldots, B_M がスペース区切りで与えられる。
- 3 行目には、質問の数 N が与えられる。
- 続く N 行のうち j 行目(1 \leq j \leq N)には、j 番目の質問の範囲を表す L_j と R_j がスペース区切りで与えられる。これは子ども L_j から子ども R_j まで(両端を含む)を意味する。
出力
N 行出力せよ。j 行目(1 \leq j \leq N)には、j 番目の質問に対する答え、すなわち子ども L_j から子ども R_j までの最終的なお菓子の個数の合計を出力せよ。
なお、答えが 32 ビット整数の範囲に収まらない場合があることに注意せよ。
入力例 1
5 13 1 2 3 4 5 3 1 5 2 4 1 3
出力例 1
28 17 15
入力例 2
3 0 5 10 15 3 1 3 1 1 2 3
出力例 2
30 5 25
入力例 3
8 25 10 20 30 40 50 60 70 80 5 1 8 1 1 3 7 1 4 5 8
出力例 3
385 14 265 113 272
入力例 4
10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 6 1 10 1 1 1 5 6 10 3 7 10 10
出力例 4
11000000000 1100000000 5500000000 5500000000 5500000000 1100000000
入力例 5
1 1000000000 1000000000 1 1 1
出力例 5
2000000000
Score : 333 pts
Problem Statement
Takahashi is going to distribute sweets to M children. The children are numbered from 1 to M. There are S sweets in total.
Each child i (1 \leq i \leq M) already has B_i sweets before the distribution.
The rules of distribution are as follows:
- Let q = \lfloor S / M \rfloor be the quotient and r = S \mod M be the remainder when S is divided by M.
- Give q sweets to every child.
- Additionally, distribute the remaining r sweets one by one to the children in order of their numbers, starting from the smallest. That is, children 1, 2, \ldots, r each receive one additional sweet (if r = 0, no additional distribution is performed).
As a result, the number of sweets child i receives in this distribution is q + 1 if i \leq r, and q if i > r.
The final number of sweets for child i is defined as the sum of the B_i sweets they originally had and the number they received in this distribution.
Takahashi must answer N questions from Aoki. For the j-th question (1 \leq j \leq N), find the sum of the final number of sweets for children from child L_j to child R_j (inclusive).
Output the answer for each of the N questions.
Constraints
- 1 \leq M \leq 2 \times 10^5
- 0 \leq S \leq 10^{9}
- 0 \leq B_i \leq 10^{9} (1 \leq i \leq M)
- 1 \leq N \leq 2 \times 10^5
- 1 \leq L_j \leq R_j \leq M (1 \leq j \leq N)
- All inputs are integers.
Input
M S B_1 B_2 \ldots B_M N L_1 R_1 L_2 R_2 \vdots L_N R_N
- The first line contains the number of children M and the total number of sweets S, separated by a space.
- The second line contains the number of sweets each child originally has, B_1, B_2, \ldots, B_M, separated by spaces.
- The third line contains the number of questions N.
- In the following N lines, the j-th line (1 \leq j \leq N) contains L_j and R_j separated by a space, representing the range of the j-th question. This means from child L_j to child R_j (inclusive).
Output
Output N lines. The j-th line (1 \leq j \leq N) should contain the answer to the j-th question, that is, the sum of the final number of sweets for children from child L_j to child R_j.
Note that the answer may not fit within the range of a 32-bit integer.
Sample Input 1
5 13 1 2 3 4 5 3 1 5 2 4 1 3
Sample Output 1
28 17 15
Sample Input 2
3 0 5 10 15 3 1 3 1 1 2 3
Sample Output 2
30 5 25
Sample Input 3
8 25 10 20 30 40 50 60 70 80 5 1 8 1 1 3 7 1 4 5 8
Sample Output 3
385 14 265 113 272
Sample Input 4
10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 6 1 10 1 1 1 5 6 10 3 7 10 10
Sample Output 4
11000000000 1100000000 5500000000 5500000000 5500000000 1100000000
Sample Input 5
1 1000000000 1000000000 1 1 1
Sample Output 5
2000000000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は果樹園でりんごの収穫作業を手伝っています。果樹園には N 本のりんごの木が一直線上に並んでおり、それぞれの木は異なる位置に植えられています。 i 番目の木は数直線上の座標 X_i の位置にあります。
高橋君は収穫用のカートを使ってりんごを集めます。カートは数直線上の区間 [L, R]( L \leq R )にまたがって停めることができ、その区間内にあるすべての木からりんごを収穫できます。ただし、カートがカバーできる範囲には制限があり、区間の長さ R - L は K 以下でなければなりません。
高橋君は、長さが K 以下の区間を 1 つ選んでカートを停め、できるだけ多くの木からりんごを収穫したいと考えています。
木 i が区間 [L, R] 内にあるとは、 L \leq X_i \leq R を満たすことを意味します。
最大で何本の木からりんごを収穫できるか求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- 0 \leq X_i \leq 10^9
- X_i \neq X_j (i \neq j)
- 入力はすべて整数
入力
N K X_1 X_2 \ldots X_N
- 1 行目には、りんごの木の本数を表す N と、区間の長さの上限を表す K が、スペース区切りで与えられる。
- 2 行目には、各りんごの木の座標を表す X_1, X_2, \ldots, X_N が、スペース区切りで与えられる。
出力
長さが K 以下の区間で収穫できるりんごの木の最大本数を 1 行で出力してください。
入力例 1
5 3 1 5 2 8 4
出力例 1
3
入力例 2
8 10 3 15 7 25 12 30 8 20
出力例 2
4
入力例 3
12 100 50 200 75 300 120 80 450 90 500 110 85 95
出力例 3
8
Score : 366 pts
Problem Statement
Takahashi is helping with the apple harvest at an orchard. The orchard has N apple trees lined up in a straight line, each planted at a distinct position. The i-th tree is located at coordinate X_i on the number line.
Takahashi uses a harvesting cart to collect apples. The cart can be parked spanning an interval [L, R] (L \leq R) on the number line, and he can harvest apples from all trees within that interval. However, there is a limit on the range the cart can cover: the length of the interval R - L must be at most K.
Takahashi wants to choose a single interval of length at most K to park the cart and harvest apples from as many trees as possible.
A tree i is within the interval [L, R] if and only if L \leq X_i \leq R.
Determine the maximum number of trees from which apples can be harvested.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^9
- 0 \leq X_i \leq 10^9
- X_i \neq X_j (i \neq j)
- All inputs are integers
Input
N K X_1 X_2 \ldots X_N
- The first line contains N, the number of apple trees, and K, the upper limit on the interval length, separated by a space.
- The second line contains the coordinates of each apple tree X_1, X_2, \ldots, X_N, separated by spaces.
Output
Print on a single line the maximum number of apple trees that can be harvested with an interval of length at most K.
Sample Input 1
5 3 1 5 2 8 4
Sample Output 1
3
Sample Input 2
8 10 3 15 7 25 12 30 8 20
Sample Output 2
4
Sample Input 3
12 100 50 200 75 300 120 80 450 90 500 110 85 95
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は砂漠の一本道を車で横断しようとしています。高橋君は現在地点 0 におり、地点 G にあるオアシスまでたどり着く必要があります。ここで、地点 x は出発地点からの距離が x である位置を表します。
高橋君の車には最初 F リットルの燃料が入っています。車は 1 リットルの燃料で距離 1 だけ進むことができ、燃料が尽きるとそれ以上進むことはできません。高橋君は地点 0 から地点 G に向かって一方向にのみ進み、後戻りすることはできません。地点 G に燃料がちょうど 0 の状態で到達した場合も、たどり着いたものとみなします。
地点 0 と地点 G の間には N 個の給油所があります。i 番目の給油所は地点 P_i(0 < P_i < G)にあり、R_i リットルの燃料を補給することができます。高橋君は各給油所について、立ち寄るか通過するかを選ぶことができます。立ち寄った場合、現在の燃料に R_i リットルが加算されます。各給油所には高々 1 回しか立ち寄ることができません。また、立ち寄った場合は必ず R_i リットル全量を補給するものとします(一部だけ補給することはできません)。なお、車の燃料タンクの容量に上限はなく、補給によって燃料がいくら増えても問題ありません。燃料がちょうど 0 の状態で給油所の地点に到達した場合でも、その給油所に立ち寄ることができます。
高橋君が地点 G のオアシスにたどり着くために、立ち寄る必要がある給油所の最小個数を求めてください。ただし、どのように給油所に立ち寄っても地点 G にたどり着けない場合は -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq G \leq 10^9
- 0 \leq F \leq 10^9
- 1 \leq P_i < G (1 \leq i \leq N)
- 1 \leq R_i \leq 10^9 (1 \leq i \leq N)
- P_i \neq P_j (i \neq j)
- 入力はすべて整数
入力
N G F P_1 R_1 P_2 R_2 \vdots P_N R_N
- 1 行目には、給油所の個数を表す N、目的地の位置を表す G、初期燃料を表す F が、スペース区切りで与えられる。
- 2 行目から N + 1 行目には、各給油所の情報が与えられる。
- i + 1 行目では、i 番目の給油所の位置 P_i と、そこで補給できる燃料の量 R_i がスペース区切りで与えられる。
出力
高橋君が地点 G にたどり着くために立ち寄る必要がある給油所の最小個数を 1 行で出力してください。たどり着けない場合は -1 を出力してください。
入力例 1
3 10 4 4 3 6 2 7 3
出力例 1
2
入力例 2
3 15 4 3 2 6 1 10 10
出力例 2
-1
入力例 3
8 60 15 10 10 14 5 20 12 25 8 32 15 40 4 45 10 52 8
出力例 3
4
入力例 4
15 120 20 58 4 8 6 90 6 35 12 107 5 18 15 73 8 50 20 13 4 98 15 42 7 81 9 27 5 65 10 113 10
出力例 4
9
入力例 5
1 2 2 1 1
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi is attempting to cross a desert along a single road by car. Takahashi is currently at point 0 and needs to reach an oasis at point G. Here, point x represents the position at distance x from the starting point.
Takahashi's car initially contains F liters of fuel. The car can travel a distance of 1 using 1 liter of fuel, and cannot proceed further once the fuel runs out. Takahashi can only travel in one direction from point 0 toward point G and cannot turn back. Arriving at point G with exactly 0 fuel remaining is still considered as having reached the destination.
There are N gas stations between point 0 and point G. The i-th gas station is located at point P_i (0 < P_i < G) and can refuel R_i liters of fuel. For each gas station, Takahashi can choose to either stop by or pass through. If he stops by, R_i liters are added to his current fuel. He can stop at each gas station at most once. Additionally, if he stops by, he must refuel the full R_i liters (partial refueling is not possible). Note that there is no upper limit on the car's fuel tank capacity, so there is no problem regardless of how much fuel increases through refueling. Even if Takahashi arrives at a gas station's location with exactly 0 fuel, he can still stop at that gas station.
Find the minimum number of gas stations Takahashi needs to stop at in order to reach the oasis at point G. If it is impossible to reach point G regardless of which gas stations he stops at, output -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq G \leq 10^9
- 0 \leq F \leq 10^9
- 1 \leq P_i < G (1 \leq i \leq N)
- 1 \leq R_i \leq 10^9 (1 \leq i \leq N)
- P_i \neq P_j (i \neq j)
- All inputs are integers
Input
N G F P_1 R_1 P_2 R_2 \vdots P_N R_N
- The first line contains N representing the number of gas stations, G representing the position of the destination, and F representing the initial fuel, separated by spaces.
- From the 2nd line to the (N + 1)-th line, the information of each gas station is given.
- The (i + 1)-th line contains the position P_i of the i-th gas station and the amount of fuel R_i that can be refueled there, separated by spaces.
Output
Output in one line the minimum number of gas stations Takahashi needs to stop at to reach point G. If it is impossible to reach, output -1.
Sample Input 1
3 10 4 4 3 6 2 7 3
Sample Output 1
2
Sample Input 2
3 15 4 3 2 6 1 10 10
Sample Output 2
-1
Sample Input 3
8 60 15 10 10 14 5 20 12 25 8 32 15 40 4 45 10 52 8
Sample Output 3
4
Sample Input 4
15 120 20 58 4 8 6 90 6 35 12 107 5 18 15 73 8 50 20 13 4 98 15 42 7 81 9 27 5 65 10 113 10
Sample Output 4
9
Sample Input 5
1 2 2 1 1
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は、ある会社の人事データを分析しています。この会社には N 人の社員がおり、各社員には数直線上の点で表される勤務地が割り当てられています。i 番目の社員の勤務地の座標は X_i、評価スコアは V_i です。なお、X_i はすべて異なります。評価スコアは大きいほど高い評価を表します。
高橋君は Q 個の調査依頼を受けています。j 番目の調査では、勤務地座標の範囲 [L_j, R_j] が指定されます。
各調査 j について、勤務地座標が L_j 以上 R_j 以下である社員の集合を S_j とします。S_j に含まれる各社員について、その社員の「順位」を次のように定めます。
- S_j の中で、その社員より評価スコアが真に大きい社員の人数を k としたとき、その社員の順位は k + 1 とする。
すなわち、同じ評価スコアの社員が複数いる場合、それらはすべて同じ順位になります(いわゆる「同率順位」の方式です)。
例: ある調査において、S_j に含まれる社員の評価スコアが 5, 5, 3 の 3 人であった場合を考えます。
- 評価スコア 5 の 2 人は、S_j の中で自分より真に大きい評価スコアを持つ社員が 0 人なので、順位はそれぞれ 0 + 1 = 1 です。
- 評価スコア 3 の 1 人は、S_j の中で自分より真に大きい評価スコアを持つ社員が 2 人なので、順位は 2 + 1 = 3 です。
よって 3 人の順位はそれぞれ 1, 1, 3 となり、順位の総和は 1 + 1 + 3 = 5 です。
各調査について、S_j に含まれるすべての社員の順位の総和を求めてください。ただし、S_j が空集合(範囲内に社員がいない場合)のときは、総和は 0 とします。
制約
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 1 \leq X_i \leq 10^9
- 1 \leq V_i \leq 10^9
- 1 \leq L_j \leq R_j \leq 10^9
- X_i はすべて異なる
- V_i は異なるとは限らない
- 入力はすべて整数である
入力
N Q X_1 V_1 X_2 V_2 \vdots X_N V_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- 1 行目には、社員の数を表す整数 N と、調査の数を表す整数 Q が、スペース区切りで与えられる。
- 続く N 行のうち i 行目には、i 番目の社員の勤務地座標 X_i と評価スコア V_i が、スペース区切りで与えられる。
- 続く Q 行のうち j 行目には、j 番目の調査の範囲の左端 L_j と右端 R_j が、スペース区切りで与えられる。
出力
Q 行出力してください。j 行目には、j 番目の調査における S_j に含まれるすべての社員の順位の総和を整数で出力してください。
入力例 1
3 3 1 5 2 5 3 3 1 3 2 3 1 1
出力例 1
5 3 1
入力例 2
4 3 1 10 3 20 5 10 7 30 2 6 8 10 1 7
出力例 2
3 0 9
入力例 3
8 5 2 7 4 3 6 7 8 5 10 3 12 9 14 7 16 1 1 16 4 12 6 6 3 9 1 2
出力例 3
32 14 1 6 1
入力例 4
10 8 5 100 11 50 17 100 23 75 29 50 35 200 41 75 47 100 53 50 59 200 1 59 5 35 20 50 11 47 35 59 1 10 29 29 17 41
出力例 4
47 19 14 25 14 1 1 14
入力例 5
1 1 1000000000 1000000000 1000000000 1000000000
出力例 5
1
Score : 466 pts
Problem Statement
Takahashi is analyzing personnel data of a certain company. The company has N employees, and each employee is assigned a workplace represented as a point on a number line. The workplace coordinate of the i-th employee is X_i, and their evaluation score is V_i. All X_i are distinct. A higher evaluation score represents a better evaluation.
Takahashi has received Q investigation requests. The j-th investigation specifies a range of workplace coordinates [L_j, R_j].
For each investigation j, let S_j be the set of employees whose workplace coordinates are at least L_j and at most R_j. For each employee in S_j, their "rank" is determined as follows:
- Let k be the number of employees in S_j whose evaluation score is strictly greater than that employee's evaluation score. Then that employee's rank is k + 1.
In other words, if multiple employees have the same evaluation score, they all receive the same rank (this is the so-called "standard competition ranking" method).
Example: Consider an investigation where S_j contains 3 employees with evaluation scores 5, 5, 3.
- The 2 employees with evaluation score 5 each have 0 employees in S_j with a strictly greater evaluation score, so their ranks are 0 + 1 = 1 each.
- The 1 employee with evaluation score 3 has 2 employees in S_j with a strictly greater evaluation score, so their rank is 2 + 1 = 3.
Thus, the ranks of the 3 employees are 1, 1, 3 respectively, and the sum of ranks is 1 + 1 + 3 = 5.
For each investigation, find the sum of ranks of all employees in S_j. If S_j is empty (no employees are within the range), the sum is 0.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 1 \leq X_i \leq 10^9
- 1 \leq V_i \leq 10^9
- 1 \leq L_j \leq R_j \leq 10^9
- All X_i are distinct
- V_i are not necessarily distinct
- All input values are integers
Input
N Q X_1 V_1 X_2 V_2 \vdots X_N V_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
- The first line contains two space-separated integers: N, the number of employees, and Q, the number of investigations.
- The following N lines each contain two space-separated values: the i-th line gives the workplace coordinate X_i and evaluation score V_i of the i-th employee.
- The following Q lines each contain two space-separated values: the j-th line gives the left endpoint L_j and right endpoint R_j of the j-th investigation's range.
Output
Output Q lines. The j-th line should contain an integer representing the sum of ranks of all employees in S_j for the j-th investigation.
Sample Input 1
3 3 1 5 2 5 3 3 1 3 2 3 1 1
Sample Output 1
5 3 1
Sample Input 2
4 3 1 10 3 20 5 10 7 30 2 6 8 10 1 7
Sample Output 2
3 0 9
Sample Input 3
8 5 2 7 4 3 6 7 8 5 10 3 12 9 14 7 16 1 1 16 4 12 6 6 3 9 1 2
Sample Output 3
32 14 1 6 1
Sample Input 4
10 8 5 100 11 50 17 100 23 75 29 50 35 200 41 75 47 100 53 50 59 200 1 59 5 35 20 50 11 47 35 59 1 10 29 29 17 41
Sample Output 4
47 19 14 25 14 1 1 14
Sample Input 5
1 1 1000000000 1000000000 1000000000 1000000000
Sample Output 5
1