#P15948. [JOI Final 2026] 面包师 / Baker

[JOI Final 2026] 面包师 / Baker

Problem Description

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

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

Manager KK, 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 tt cannot fulfill orders from customers jj (1jM1 \leq j \leq M) such that Tj>tT_j > t.

Manager KK is currently considering QQ work plans. The qq-th plan (1qQ1 \leq q \leq Q) is to have baker AqA_q start work at time BqB_q. To help with the decision, for each of the QQ 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 Format

Read the following data from the standard input.

NN MM LL QQ
T1T_1 T2T_2 \cdots TMT_M
A1A_1 B1B_1
A2A_2 B2B_2
\vdots
AQA_Q BQB_Q

Output Format

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

4 4 6 4
0 2 3 8
2 3
1 6
3 3
4 7
3
2
2
0
20 5 12 4
1 2 4 8 10
1 12
3 10
3 11
15 10
5
4
3
0
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
2
2
4
3
1
2
5
0
3
2

Hint

Sample 1

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

  • First, to fulfill customer 11’s order, start making a croissant at time 33 and finish it 22 minutes later at time 55. (This satisfies the condition to finish by time T1+L=0+6=6T_1 + L = 0 + 6 = 6.)
  • Next, to fulfill customer 22’s order, start making a croissant at time 55 and finish it 22 minutes later at time 77. (This satisfies the condition to finish by time T2+L=2+6=8T_2 + L = 2 + 6 = 8.)
  • Finally, to fulfill customer 33’s order, start making a croissant at time 77 and finish it 22 minutes later at time 99. (This satisfies the condition to finish by time T3+L=3+6=9T_3 + L = 3 + 6 = 9.)

Customer 44’s order is ignored because it arrives after the baker’s start time, so it cannot be fulfilled. Thus, a maximum of 33 customers’ orders can be fulfilled, so the 11st line outputs 33.

Regarding the 22nd work plan, baker 11 starting at time 66 can fulfill the orders of 22 customers 22 and 33, for example, as follows:

  • First, to fulfill customer 33’s order, start making a croissant at time 66 and finish it 11 minute later at time 77. (This satisfies the condition to finish by time T3+L=3+6=9T_3 + L = 3 + 6 = 9.)
  • Next, to fulfill customer 22’s order, start making a croissant at time 77 and finish it 11 minute later at time 88. (This satisfies the condition to finish by time T2+L=2+6=8T_2 + L = 2 + 6 = 8.)

Customer 44’s order is ignored because it arrives after the start time. Also, the order of customer 11, which needs to be finished by time 66, cannot be fulfilled. Thus, a maximum of 22 customers’ orders can be fulfilled, so the 22nd line outputs 22.

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

Regarding the 44th work plan, baker 44 starting at time 77 cannot fulfill any customer’s order. Thus, the 44th line outputs 00.

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

Sample 2

This sample input satisfies the constraints of all the subtasks.

Sample 3

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

Constraints

  • 1N4×10121 \leq N \leq 4 \times 10^{12}.
  • 1M20000001 \leq M \leq 2\,000\,000.
  • 1L2×10121 \leq L \leq 2 \times 10^{12}.
  • 1Q4000001 \leq Q \leq 400\,000.
  • 0Tj2×10120 \leq T_j \leq 2 \times 10^{12} (1jM1 \leq j \leq M).
  • TjTj+1T_j \leq T_{j+1} (1jM11 \leq j \leq M-1).
  • 1AqN1 \leq A_q \leq N (1qQ1 \leq q \leq Q).
  • 0Bq4×10120 \leq B_q \leq 4 \times 10^{12} (1qQ1 \leq q \leq Q).
  • Given values are all integers.

Subtasks

  1. (8 points) M10,Q100000M \leq 10,Q \leq 100000.
  2. (12 points) M500,Q100000M \leq 500,Q \leq 100000.
  3. (30 points) TMBq<T1+LT_M \leq B_q < T_1 + L (1qQ1 \leq q \leq Q).
  4. (10 points) TMBqT_M \leq B_q (1qQ1 \leq q \leq Q).
  5. (22 points) M500000,Q100000M \leq 500000,Q \leq 100000.
  6. (18 points) No additional constraints.