This page is still under construction.

Parts of this page are still being built. What you see may change.

Nemmo Nemmo 2020

Time limit3sMemory limit1024 MB

Summary
The board holds rows of nemmo forming a nonincreasing staircase; for each query (x, y), count the nemmo removed by a laser firing up column x and right along row y.
Level

Medium7 of 10

Topics
Binary search, Prefix sum, Implementation, Math
Solved
No attempts yet

Problem

Mysterious creatures called "nemmo" have started living on an old Tetris board. The board is 10910^9 cells wide and NN floors tall, and each nemmo occupies one cell on one floor. For convenience, let (x,y)(x, y) denote the cell in the xx-th column from the left and the yy-th floor from the bottom.

Floor yy contains aya_y nemmo. Since nemmo like to stay close together, they live side by side in cells (1,y),…,(ay,y)(1, y), \dots, (a_y, y), and because they are affected by gravity, ay≥ay+1a_y \ge a_{y+1} for every 1≤y≤N−11 \le y \le N - 1.

nemmo

Nemmo living on the Tetris board. Here N=3N = 3, a1=3a_1 = 3, a2=3a_2 = 3, and a3=2a_3 = 2.

Leff, who wants to play Tetris, plans to clear the nemmo away with a laser. Installing a laser at (x,y)(x, y) makes all nemmo disappear that live in the xx-th column from the left on floor yy or above, along with all nemmo on floor yy to the right of (x,y)(x, y). No other nemmo disappears right away.

laser

A laser installed at (1,2)(1, 2). A total of 4 nemmo are hit by the laser and disappear.

There are QQ positions where a laser can be installed. For each position, tell Leff how many nemmo would be removed if a laser were installed there. Since this is only planning an installation rather than actually installing lasers, the plans do not affect one another.

Input

The first line contains two integers NN and QQ separated by a space. NN is the height of the board, and QQ is the number of positions where a laser can be installed.

The second line contains NN integers a1,…,aNa_1, \dots, a_N separated by spaces. This means that floor ii contains aia_i nemmo.

Each of the next QQ lines gives a position where a laser can be installed. The (i+2)(i + 2)-th line contains two integers xix_i and yiy_i separated by a space, meaning a laser can be installed at (xi,yi)(x_i, y_i).

Output

Print the answers over QQ lines. The ii-th line should contain the number of nemmo removed by installing a laser at (xi,yi)(x_i, y_i).

Constraints

  • 1≤N,Q≤250,0001 \le N, Q \le 250{,}000
  • 1≤ai≤1091 \le a_i \le 10^9 (1≤i≤N1 \le i \le N)
  • a1≥a2≥⋯≥aNa_1 \ge a_2 \ge \dots \ge a_N
  • 1≤xi≤1091 \le x_i \le 10^9, 1≤yi≤n1 \le y_i \le n (1≤i≤Q1 \le i \le Q)

Examples1

  1. Example 1

    Input
    3 11
    3 3 2
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    3 1
    3 2
    4 1
    4 2
    3 3
    
    Expected output
    5
    4
    2
    4
    3
    1
    2
    1
    0
    0
    0