J - パン職人 (Baker) 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

Score: 100 points

Problem Statement

JOI Bakery is a bakery famous for its mouth-watering croissants. JOI Bakery has N bakers numbered from 1 to N. Baker i (1 \leq i \leq N) takes i minutes to make one croissant. One baker cannot make multiple croissants at the same time.

Today, M customers numbered from 1 to M are scheduled to visit JOI Bakery, and each customer plans to order one croissant. Customer j (1 \leq j \leq M) will order a croissant at time T_j, where time t denotes the time t minutes from now. However, a customer who cannot receive their croissant within L minutes of ordering will give up and leave the shop. In other words, to fulfill the order of customer j (1 \leq j \leq M), the croissant must be finished by time T_j + L (including exactly at time T_j + L).

Manager K, who manages JOI Bakery, plans to have exactly one baker work today and is considering which baker to send and at what time. Since bakers focus intensely on bread-making during their shift, they ignore all orders placed after their start time (not including exactly at the start time). That is, a baker starting work at time t cannot fulfill orders from customers j (1 \leq j \leq M) such that T_j > t.

Manager K is currently considering Q work plans. The q-th plan (1 \leq q \leq Q) is to have baker A_q start work at time B_q. To help with the decision, for each of the Q plans, he wants to know the maximum number of customers whose orders can be fulfilled if that plan is executed. Note that the time it takes for a baker to start making a croissant after arriving, and the time it takes to start making a new croissant after finishing one, can be ignored.

Given information about the customers visiting JOI Bakery and the work plans, create a program to find the maximum number of customers whose orders can be fulfilled for each plan.


Input

Read the following data from the standard input.

N M L Q
T_1 T_2 \cdots T_M
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

Output

Write Q lines to the standard output. The q-th line (1 \leq q \leq Q) of the output should contain an integer representing the maximum number of customers whose orders can be fulfilled in the q-th work plan.


Constraints

  • 1 \leq N \leq 4\times 10^{12}.
  • 1 \leq M \leq 2\,000\,000.
  • 1 \leq L \leq 2\times 10^{12}.
  • 1 \leq Q \leq 400\,000.
  • 0 \leq T_j \leq 2\times 10^{12} (1 \leq j \leq M).
  • T_j \leq T_{j+1} (1 \leq j \leq M-1).
  • 1 \leq A_q \leq N (1 \leq q \leq Q).
  • 0 \leq B_q \leq 4\times 10^{12} (1 \leq q \leq Q).
  • Given values are all integers.

Subtasks

  1. (8 points) M \leq 10, Q \leq 100\,000.
  2. (12 points) M \leq 500, Q \leq 100\,000.
  3. (30 points) T_M \leq B_q < T_1+L (1 \leq q \leq Q).
  4. (10 points) T_M \leq B_q (1 \leq q \leq Q).
  5. (22 points) M \leq 500\,000, Q \leq 100\,000.
  6. (18 points) No additional constraints.

Sample Input 1

4 4 6 4
0 2 3 8
2 3
1 6
3 3
4 7

Sample Output 1

3
2
2
0

Regarding the 1st work plan, baker 2 starting at time 3 can fulfill the orders of 3 customers 1, 2, and 3, for example, as follows:

  • First, to fulfill customer 1's order, start making a croissant at time 3 and finish it 2 minutes later at time 5. (This satisfies the condition to finish by time T_1 + L = 0 + 6 = 6.)
  • Next, to fulfill customer 2's order, start making a croissant at time 5 and finish it 2 minutes later at time 7. (This satisfies the condition to finish by time T_2 + L = 2 + 6 = 8.)
  • Finally, to fulfill customer 3's order, start making a croissant at time 7 and finish it 2 minutes later at time 9. (This satisfies the condition to finish by time T_3 + L = 3 + 6 = 9.)

Customer 4's order is ignored because it arrives after the baker's start time, so it cannot be fulfilled. Thus, a maximum of 3 customers' orders can be fulfilled, so the 1st line outputs 3.

Regarding the 2nd work plan, baker 1 starting at time 6 can fulfill the orders of 2 customers 2 and 3, for example, as follows:

  • First, to fulfill customer 3's order, start making a croissant at time 6 and finish it 1 minute later at time 7. (This satisfies the condition to finish by time T_3 + L = 3 + 6 = 9.)
  • Next, to fulfill customer 2's order, start making a croissant at time 7 and finish it 1 minute later at time 8. (This satisfies the condition to finish by time T_2 + L = 2 + 6 = 8.)

Customer 4's order is ignored because it arrives after the start time. Also, the order of customer 1, which needs to be finished by time 6, cannot be fulfilled. Thus, a maximum of 2 customers' orders can be fulfilled, so the 2nd line outputs 2.

Regarding the 3rd work plan, baker 3 starting at time 3 can fulfill orders for customers 1 and 3, or customers 2 and 3, but cannot fulfill all orders for customers 1, 2, and 3. They also cannot fulfill the order for customer 4 which arrives after the start time. Thus, a maximum of 2 customers' orders can be fulfilled, so the 3rd line outputs 2.

Regarding the 4th work plan, baker 4 starting at time 7 cannot fulfill any customer's order. Thus, the 4th line outputs 0.

This sample input satisfies the constraints of Subtasks 1,2,5, and 6.


Sample Input 2

20 5 12 4
1 2 4 8 10
1 12
3 10
3 11
15 10

Sample Output 2

5
4
3
0

This sample input satisfies the constraints of all the subtasks.


Sample Input 3

100000 6 272273 10
5 9 209 8128 17202 50102
164 9
11 24
835 9267
2 256
2 314156
18475 142
1826 18978
44757 1
4 1646
218 44

Sample Output 3

2
2
4
3
1
2
5
0
3
2

This sample input satisfies the constraints of Subtasks 1,2,5, and 6.

配点: 100

問題文

JOI ベーカリーは頬が落ちるほど美味しいクロワッサンで有名なパン屋さんである. JOI ベーカリーには 1 から N までの番号が付けられた N 人のパン職人が在籍しており,職人 i (1\leqq i \leqq N) は 1 つのクロワッサンを作るのに i 分かかる. 1 人の職人が同時に複数のクロワッサンを作ることはできない.

JOI ベーカリーには今日これから,1 から M までの番号が付けられた M 人の客が来店し,それぞれの客がクロワッサンを 1 つ注文する予定である. 今から t 分後のことを時刻 t と呼ぶことにすると,客 j (1\leqq j \leqq M) がクロワッサンを注文するのは時刻 T_j である. ただし,注文後 L 分以内にクロワッサンを受け取ることのできなかった客は,諦めて店から去ってしまう. すなわち,客 j (1\leqq j \leqq M) の注文に応えるには,時刻 T_j+L まで(時刻 T_j+L ちょうどを含む)にクロワッサンを作り終えなければならない.

JOI ベーカリーを管理する K 店長は,今日はちょうど 1 人の職人を店に出勤させることを予定しており,どの職人をどの時刻に出勤させるのがよいか考えている. 職人たちは勤務中はパン作りに集中してしまうため,出勤時刻より後(出勤時刻ちょうどを含まない)に入った注文をすべて無視してしまう. すなわち,時刻 t に出勤する職人は,T_j > t を満たすような客 j (1\leqq j \leqq M) の注文に応えることができない.

K 店長は現在 Q 個の出勤案を検討しており,q 番目の案 (1\leqq q \leqq Q) は職人 A_q を時刻 B_q に出勤させるというものである. 検討の材料とするため,Q 個の案それぞれについて,その案を実行した際に最大で何人の客の注文に応えることができるか知りたい. なお,職人が出勤してからクロワッサンを作り始めるまでにかかる時間や,クロワッサンを 1 つ作り終えてから新しいクロワッサンを作り始めるまでにかかる時間は無視できるものとする.

JOI ベーカリーに来店する客と出勤案の情報が与えられたとき,それぞれの案において最大で何人の客の注文に応えることができるかを求めるプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N M L Q
T_1 T_2 \cdots T_M
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

出力

標準出力に Q 行で出力せよ. q 行目 (1\leqq q \leqq Q) には,q 番目の出勤案において最大で何人の客の注文に応えることができるかを表す整数を出力せよ.


制約

  • 1 \leqq N \leqq 4\times 10^{12}
  • 1 \leqq M \leqq 2\,000\,000
  • 1 \leqq L \leqq 2\times 10^{12}
  • 1 \leqq Q \leqq 400\,000
  • 0 \leqq T_j \leqq 2\times 10^{12} (1 \leqq j \leqq M).
  • T_j \leqq T_{j+1} (1 \leqq j \leqq M-1).
  • 1 \leqq A_q \leqq N (1 \leqq q \leqq Q).
  • 0 \leqq B_q \leqq 4\times 10^{12} (1 \leqq q \leqq Q).
  • 入力される値はすべて整数である.

小課題

  1. (8 点) M \leqq 10Q \leqq 100\,000
  2. (12 点) M \leqq 500Q \leqq 100\,000
  3. (30 点) T_M \leqq B_q < T_1+L (1 \leqq q \leqq Q).
  4. (10 点) T_M \leqq B_q (1 \leqq q \leqq Q).
  5. (22 点) M \leqq 500\,000Q \leqq 100\,000
  6. (18 点) 追加の制約はない.

入力例 1

4 4 6 4
0 2 3 8
2 3
1 6
3 3
4 7

出力例 1

3
2
2
0

1 番目の出勤案について,時刻 3 に出勤した職人 2 は,例えば以下のようにして 3 人の客 1,2,3 の注文に応えることができる.

  • まず,客 1 の注文に応えるため,時刻 3 にクロワッサンを 1 つ作り始め,2 分後の時刻 5 に作り終える.(これは,時刻 T_1+L=0+6=6 までに作り終えるという条件を満たしている.)
  • 次に,客 2 の注文に応えるため,時刻 5 にクロワッサンを 1 つ作り始め,2 分後の時刻 7 に作り終える.(これは,時刻 T_2+L=2+6=8 までに作り終えるという条件を満たしている.)
  • 最後に,客 3 の注文に応えるため,時刻 7 にクロワッサンを 1 つ作り始め,2 分後の時刻 9 に作り終える.(これは,時刻 T_3+L=3+6=9 までに作り終えるという条件を満たしている.)

4 の注文は出勤時刻よりも後に入るため無視してしまい,応えることができない. よって,最大で 3 人の客の注文に応えることができ,1 行目には 3 を出力する.

2 番目の出勤案について,時刻 6 に出勤した職人 1 は,例えば以下のようにして 2 人の客 2,3 の注文に応えることができる.

  • まず,客 3 の注文に応えるため,時刻 6 にクロワッサンを 1 つ作り始め,1 分後の時刻 7 に作り終える.(これは,時刻 T_3+L=3+6=9 までに作り終えるという条件を満たしている.)
  • 次に,客 2 の注文に応えるため,時刻 7 にクロワッサンを 1 つ作り始め,1 分後の時刻 8 に作り終える.(これは,時刻 T_2+L=2+6=8 までに作り終えるという条件を満たしている.)

4 の注文は出勤時刻よりも後に入るため無視してしまい,応えることができない. また,時刻 6 までに作り終える必要のある客 1 の注文にも応えることができない. よって,最大で 2 人の客の注文に応えることができ,2 行目には 2 を出力する.

3 番目の出勤案について,時刻 3 に出勤した職人 3 は,客 1,3 の注文に応えることや客 2,3 の注文に応えることはできるが, 客 1,2,3 の注文すべてに応えることはできず,また出勤時刻よりも後に入る客 4 の注文にも応えることができない. よって,最大で 2 人の客の注文に応えることができ,3 行目には 2 を出力する.

4 番目の出勤案について,時刻 7 に出勤した職人 4 は,どの客の注文にも応えることができない. よって,4 行目には 0 を出力する.

この入力例は小課題 1,2,5,6 の制約を満たす.


入力例 2

20 5 12 4
1 2 4 8 10
1 12
3 10
3 11
15 10

出力例 2

5
4
3
0

この入力例はすべての小課題の制約を満たす.


入力例 3

100000 6 272273 10
5 9 209 8128 17202 50102
164 9
11 24
835 9267
2 256
2 314156
18475 142
1826 18978
44757 1
4 1646
218 44

出力例 3

2
2
4
3
1
2
5
0
3
2

この入力例は小課題 1,2,5,6 の制約を満たす.