Under construction

Still being built. Things may change

Catching Water Bottles

Time limit2sMemory limit256 MB

Summary
Given N bottles with thresholds D_i, count how many bottles plus Jaehyeok lie within [L, R] at time T as the bottles trail him each second.
Level

Hard8 of 10

Topics
Segment tree, Simulation, Array
Solved
No attempts yet

Problem

Jaehyeok has an unusual hobby. At time TT, he tastes the water of every bottle whose position is between LL and RR, inclusive. But he had been sitting still and drinking water in place, and he worried about becoming obese, so he decided to move. The bottles are free-spirited and move when a certain condition holds.

The bottles are numbered 1,2,…,N1, 2, \ldots, N, and the initial position of bottle ii is −i-i. Jaehyeok starts at 00. Every second, Jaehyeok and the bottles move according to these rules.

  1. Jaehyeok moves 11 to the right every second.
  2. Bottles do not move carelessly, since the water might spill. After Jaehyeok moves, bottle 11 moves to (Jaehyeok's position) −1-1 if its distance from Jaehyeok is at least D1+1D_1+1.
  3. After bottle 11's position is decided, bottle 22 moves to (bottle 11's position) −1-1 if its distance from bottle 11 is at least D2+1D_2+1. Bottles 3,4,…,N3, 4, \ldots, N move in the same way, in order. That is, bottle ii moves to (bottle i−1i-1's position) −1-1 if its distance from bottle i−1i-1 is at least Di+1D_i+1.

Jaehyeok drank so much water that he began to wonder how many bottles he can catch, given TT, LL, and RR. He had also started to think he was a bottle himself. So if Jaehyeok's position is between LL and RR inclusive, add 11 to the answer.

Input

The first line contains the number of bottles NN (1≤N≤500,0001 \le N \le 500{,}000) and the number of queries QQ (1≤Q≤500,0001 \le Q \le 500{,}000). The next NN lines each contain DiD_i (1≤Di≤1091 \le D_i \le 10^9). The next QQ lines each contain TiT_i, LiL_i, and RiR_i (1≤Ti,Li≤1091 \le T_i, L_i \le 10^9, Li≤Ri≤109L_i \le R_i \le 10^9).

Output

Print QQ lines. On line ii, print how many bottles are between LiL_i and RiR_i inclusive at time TiT_i.

Examples2

  1. Example 1

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

    Input
    4 2
    1
    1
    1
    1
    2 1 4
    1 3 6
    
    Expected output
    2
    0