#include <bits/stdc++.h>
using namespace std;
const int Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 1 << 19;
struct Segtree {
  int dt[maxn << 1];
  void inline add(int x) {
    x += maxn;
    dt[x]++;
    for (x >>= 1; x; x >>= 1) dt[x] = dt[x << 1] + dt[x << 1 | 1];
  }
  int inline find(int x, int now = 1, int nl = 0, int nr = maxn - 1) {
    if (dt[now] < x || x <= 0) return -1;
    if (nl == nr) return nl;
    int m = (nl + nr) >> 1;
    if (dt[now << 1] >= x)
      return find(x, now << 1, nl, m);
    else
      return find(x - dt[now << 1], now << 1 | 1, m + 1, nr);
  }
} seg;
struct Preseg {
  int dt[1 << 23], l[1 << 23], r[1 << 23], nds;
  void inline add(int to, int &now, int nl = 0, int nr = maxn - 1) {
    nds++;
    dt[nds] = dt[now];
    l[nds] = l[now];
    r[nds] = r[now];
    now = nds;
    if (nl == nr) return void(dt[now]++);
    int m = (nl + nr) >> 1;
    if (m >= to)
      add(to, l[now], nl, m);
    else
      add(to, r[now], m + 1, nr);
    dt[now] = dt[l[now]] + dt[r[now]];
  }
  int inline sum(int L, int R, int now, int nl = 0, int nr = maxn - 1) {
    if (!now) return 0;
    if (nl >= L && nr <= R) return dt[now];
    if (nl > R || nr < L) return 0;
    int m = (nl + nr) >> 1;
    return sum(L, R, l[now], nl, m) + sum(L, R, r[now], m + 1, nr);
  }
} ps;
int n, q;
int a[1 << 19];
int id[1 << 19], rt[1 << 19];
int main() {
  scanf("%d%d", &n, &q);
  int cnt = 0;
  for (int i = 1; i <= (n); i++) {
    scanf("%d", a + i);
    int xj = i - a[i];
    id[i] = -1;
    if (xj < 0) continue;
    cnt++;
    if (xj == 0) {
      seg.add(id[i] = i - 1);
      continue;
    }
    id[i] = seg.find(cnt - xj);
    if (~id[i])
      seg.add(id[i]);
    else
      cnt--;
  }
  for (int i = 1; i <= (n); i++) {
    rt[i] = rt[i - 1];
    if (~id[i]) ps.add(id[i], rt[i]);
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    r = n - r;
    printf("%d\n", ps.sum(l, r, rt[r]));
  }
  return 0;
}
