Tower

시간 제한2초메모리 제한1024 MB

요약
막힌 계단 구간과 두 가지 이동 비용이 주어질 때, 0번 계단에서 각 질의 계단까지 오르는 최소 시간을 구하고 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 그리디, 이분 탐색, 구현
정답자
아직 제출이 없습니다

문제

The IOI Tower is an extremely tall tower equipped with a staircase for ascending. This staircase consists of 1010010^{100} steps, numbered sequentially from the bottom as step 00, step 11, and so on. JOI-kun is currently on step 00 and intends to climb the staircase. JOI-kun can ascend the staircase by taking the following 22 types of actions. Descending the staircase is not permitted.

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

Currently, construction is ongoing at several locations on the staircase, and steps undergoing construction cannot be stepped on. Specifically, there are NN ongoing constructions, and the ii-th construction (1≤i≤N1 ≤ i ≤ N) is being carried out at steps L_i,L_i+1,…,R_iL\_i , L\_{i+1}, \dots , R\_i.

The IOI Tower has QQ rooms numbered from 11 to QQ. One can enter room jj (1≤j≤Q1 ≤ j ≤ Q) from step X_jX\_j of the staircase. Therefore, JOI-kun has decided to determine whether he can reach each room and, if possible, how many seconds it will take to reach there in the minimum time.

Given the information about JOI-kun, constructions, and rooms, create a program that determines whether JOI-kun can reach step X_jX\_j for each jj (1≤j≤Q1 ≤ j ≤ Q) and, if possible, calculates the minimum time it takes.

입력

Read the following data from the standard input.

NN QQ

DD AA BB

L_1L\_1 R_1R\_1

L_2L\_2 R_2R\_2

⋮\vdots

L_NL\_N R_NR\_N

X_1X\_1

X_2X\_2

⋮\vdots

X_QX\_Q

출력

Output QQ lines to the standard output. On the jj-th line (1≤j≤Q1 ≤ j ≤ Q), output the minimum number of seconds it takes if JOI-kun can reach step X_jX\_j; otherwise, output -1.

제한

  • 1≤N≤200,0001 ≤ N ≤ 200\\, 000.
  • 1≤Q≤200,0001 ≤ Q ≤ 200\\, 000.
  • 1≤D≤10121 ≤ D ≤ 10^{12}.
  • 1≤A≤1,000,0001 ≤ A ≤ 1\\, 000\\, 000.
  • 1≤B≤1,000,0001 ≤ B ≤ 1\\, 000\\, 000.
  • 1≤L_i≤R_i≤10121 ≤ L\_i ≤ R\_i ≤ 10^{12} (1≤i≤N1 ≤ i ≤ N).
  • R_i+1<L_i+1R\_i + 1 < L\_{i+1} (1≤i≤N−11 ≤ i ≤ N - 1).
  • 1≤X_j≤10121 ≤ X\_j ≤ 10^{12} (1≤j≤Q1 ≤ j ≤ Q).
  • Given values are all integers.

예제2

  1. 예제 1

    입력
    3 1
    4 10 35
    4 5
    10 12
    14 14
    13
    
    예상 출력
    120
    
  2. 예제 2

    입력
    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