#include <bits/stdc++.h>
using namespace std;
const int md = int(1e9) + 7;
const int inf = 0x3f3f3f3f;
const long long infl = 0x3f3f3f3f3f3f3f3f;
const int mx = 1 << 19;
int tree[2 * mx], flag[2 * mx];
void update(int i) {
  while (i /= 2) tree[i] = tree[i << 1] + tree[i << 1 | 1];
}
int query(int i, int l, int r, int u, int v) {
  if (u <= l && r <= v) return tree[i];
  int mid = (l + r) / 2;
  int lans = 0, rans = 0;
  if (u < mid) lans = query(i << 1, l, mid, u, v);
  if (mid < v) rans = query(i << 1 | 1, mid, r, u, v);
  return lans + rans;
}
int lower_bound_rtol(int pos, long long sm) {
  int i = mx + pos, l = pos, len = 1;
  long long tot = 0;
  while (l >= 0) {
    if (i & 1 && tree[i >> 1] + tot < sm)
      i >>= 1, l -= len, len <<= 1;
    else if (tree[i] + tot < sm)
      tot += tree[i--], l -= len;
    else if (len > 1)
      i = i << 1 | 1, len >>= 1, l += len;
    else
      break;
  }
  return l;
}
int lower_bound_rtol(long long sm) {
  int l = mx;
  long long tot = 0;
  for (int i = 1, len = mx; i && len; i = i << 1 | 1, len >>= 1) {
    if (tot + tree[i] < sm) {
      tot += tree[i];
      l -= len;
      i--;
    }
  }
  return l - 1;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> a(n);
  for (int i = 0; i < (n); i++) {
    scanf("%d", &(a[i]));
  }
  vector<tuple<int, int, int>> vq;
  for (int i = 0; i < (q); i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    vq.push_back(make_tuple(n - r, l, i));
  }
  sort((vq).begin(), (vq).end());
  vector<int> vans(q);
  int it = 0;
  for (int i = 0; i < (n); i++) {
    int d = i + 1 - a[i];
    if (d >= 0) {
      int l = -1;
      l = lower_bound_rtol(d);
      l = min(l, i);
      if (l >= 0) {
        tree[mx + l]++;
        update(mx + l);
      }
    }
    while (it < q && get<0>(vq[it]) == i + 1) {
      int curr, curl, curi;
      tie(curr, curl, curi) = vq[it];
      int ans = 0;
      ans = query(1, 0, mx, curl, curr);
      vans[curi] = ans;
      it++;
    }
  }
  for (int i = 0; i < (q); i++) {
    printf("%d\n", vans[i]);
  }
  return 0;
}
