#P10438. [JOIST 2024] 塔楼 / Tower

[JOIST 2024] 塔楼 / Tower

Problem Description

The IOI Tower is an extremely tall tower with a staircase for going up. This staircase has 1010010^{100} steps, numbered starting from the bottom as step 00, step 11, and so on. JOI is currently at step 00 and plans to climb the staircase. JOI can move upward in the following two ways, and moving downward is not allowed.

  • Move up by 11 step. This action takes AA seconds.
  • Jump from the current step to the step DD steps above, skipping the steps in between. This action takes BB seconds.

Currently, some parts of the staircase are under construction, and steps under construction cannot be stepped on. More precisely, there are NN construction sections; in the ii-th section (1iN1 \leq i \leq N), the steps under construction are from LiL_i to RiR_i.

The IOI Tower has QQ rooms, numbered from 11 to QQ. People can enter room jj from step XjX_j of the staircase (1jQ1 \leq j \leq Q). Therefore, JOI decides to determine whether he can reach each room, and if possible, the minimum number of seconds needed.

Given the information about JOI, the construction, and the rooms, write a program that determines whether JOI can reach room jj (1jQ1 \leq j \leq Q), and if possible, computes the minimum time required.

Input Format

Read the following data from standard input.

  • NN QQ
  • DD AA BB
  • L1L_1 R1R_1
  • L2L_2 R2R_2
  • ...
  • LNL_N RNR_N
  • X1X_1
  • X2X_2
  • ...
  • XQX_Q

Output Format

Output QQ lines. On the jj-th line (1jQ1 \leq j \leq Q), output the minimum number of seconds needed for JOI to reach step XjX_j; if it is impossible to reach it, output 1-1.

3 1
4 10 35
4 5
10 12
14 14
13
120
5 10
10 1 9
7 11
25 32
37 38
43 44
50 52
6
12
18
24
30
36
42
48
54
60
6
11
17
22
-1
33
-1
44
-1
55

Hint

Sample Explanation 1

JOI can reach step 1313 of the staircase in 120120 seconds by the following steps:

  • Move up from step 00 to step 11. This takes 1010 seconds.
  • Move up from step 11 to step 22. This takes 1010 seconds.
  • Move up from step 22 to step 33. This takes 1010 seconds.
  • Jump from step 33 to step 77, skipping the steps in between. This takes 3535 seconds.
  • Move up from step 77 to step 88. This takes 1010 seconds.
  • Move up from step 88 to step 99. This takes 1010 seconds.
  • Jump from step 99 to step 1313, skipping the steps in between. This takes 3535 seconds.

Since it is impossible to reach step 1313 in less than 120120 seconds, the output is 120120.

This sample input satisfies the constraints of Subtasks 1,2,41, 2, 4.

Sample Explanation 2

This sample input satisfies the constraints of Subtasks 1,2,41, 2, 4.

Constraints

  • 1N200,0001 \leq N \leq 200,000
  • 1Q200,0001 \leq Q \leq 200,000
  • 1D10121 \leq D \leq 10^{12}
  • 1A1,000,0001 \leq A \leq 1,000,000
  • 1B1,000,0001 \leq B \leq 1,000,000
  • 1LiRi10121 \leq L_i \leq R_i \leq 10^{12}1iN1 \leq i \leq N
  • Ri+1<Li+1R_{i}+1 < L_{i+1}1iN11 \leq i \leq N-1
  • 1Xj10121 \leq X_j \leq 10^{12}1jQ1 \leq j \leq Q
  • All given values are integers.

Subtasks

  1. (5 points) Ri1,000,000R_i \leq 1,000,000 (1iN1 \leq i \leq N), Xj1,000,000X_j \leq 1,000,000 (1jQ1 \leq j \leq Q).
  2. (38 points) N2,000N \leq 2,000, Q2,000Q \leq 2,000.
  3. (25 points) A=1A = 1, B=DB = D.
  4. (32 points) No additional constraints.

Translated by ChatGPT 5