Under construction

Still being built. Things may change

Fish 3

Time limit2sMemory limit1024 MB

Summary
For each range of fish, find the fewest type A foods needed to match the target intelligence values, or -1 if the targets cannot be reached.
Level

Hard8 of 10

Topics
Greedy, Math, Array
Solved
No attempts yet

Problem

JOI-kun is breeding NN fish in a large tank. Each fish is numbered from 11 to NN.

JOI-kun has two types of fish food, A and B, and both are available in sufficient quantities. Each time one piece of food is added to the tank, exactly one fish eats it, and any fish can eat it. The intelligence of each fish changes depending on the food type and the fish that ate it:

  • When fish kk (1≤k≤N1 \le k \le N) eats one piece of type A food, the intelligence of fish kk increases by exactly DD.
  • When fish kk (1≤k≤N1 \le k \le N) eats one piece of type B food, the intelligence of every fish numbered kk and above increases by exactly 11.

At the start, every fish has intelligence 00. JOI-kun wants the intelligence of fish ii (1≤i≤N1 \le i \le N) to equal its ideal intelligence CiC_i, but this is not always possible.

JOI-kun has QQ questions. The jj-th question asks the following. Starting from intelligence 00 for all fish, and repeating the action of adding food zero or more times, can fish Lj,Lj+1,…,RjL_j, L_j+1, \dots, R_j all reach exactly their ideal intelligence values? If so, what is the minimum number of pieces of type A food that must be added to the tank?

Write a program that answers these questions, given the information about the fish and the questions.

Input

The first line contains NN and DD.

The second line contains C1,C2,⋯ ,CNC_1, C_2, \cdots, C_N.

The third line contains QQ.

Each of the next QQ lines contains LjL_j and RjR_j.

NN DD

C1C_1 C2C_2 ⋯\cdots CNC_N

QQ

L1L_1 R1R_1

L2L_2 R2R_2

⋮\vdots

LQL_Q RQR_Q

Output

Print QQ lines. On line jj, print the minimum number of pieces of type A food needed to make fish Lj,…,RjL_j, \dots, R_j have exactly their ideal intelligence values. If it is impossible, print -1.

Constraints

  • 1≤N≤300 0001 \le N \le 300\,000.
  • 1≤Q≤300 0001 \le Q \le 300\,000.
  • 1≤D≤10121 \le D \le 10^{12}.
  • 0≤Ci≤10120 \le C_i \le 10^{12} (1≤i≤N1 \le i \le N).
  • 1≤Lj≤Rj≤N1 \le L_j \le R_j \le N (1≤j≤Q1 \le j \le Q).
  • All given values are integers.

Examples4

  1. Example 1

    Input
    4 2
    3 1 2 1
    1
    1 3
    
    Expected output
    1
    
  2. Example 2

    Input
    4 2
    0 1 0 1
    3
    1 2
    2 3
    1 1
    
    Expected output
    0
    -1
    0
    
  3. Example 3

    Input
    5 1
    3 1 4 1 5
    3
    1 5
    2 4
    3 5
    
    Expected output
    5
    3
    3
    
  4. Example 4

    Input
    6 3
    16 14 13 8 6 5
    4
    1 4
    2 5
    3 3
    1 6
    
    Expected output
    9
    8
    0
    -1