#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MXN = 300001;
int A[MXN];
vector<int> byR[MXN];
int L[MXN];
int ans[MXN];
const int p2 = 524288;
int st[p2 + p2];
int query(int l, int r) {
  int ret = 0;
  l += p2;
  r += p2;
  while (l <= r) {
    if (l % 2 == 1) ret += st[l++];
    if (r % 2 == 0) ret += st[r--];
    l /= 2;
    r /= 2;
  }
  return ret;
}
void upd(int k) {
  k += p2;
  st[k]++;
  k /= 2;
  while (k > 0) {
    st[k] = st[k + k] + st[k + k + 1];
    k /= 2;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> A[i];
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x >> y;
    int l = x + 1;
    int r = n - y;
    byR[r].push_back(i);
    L[i] = l;
  }
  for (int i = 1; i <= n; i++) {
    if (A[i] > i || i - query(0, i) > A[i]) {
    } else {
      int pass = 0;
      int fail = i;
      while (fail - pass > 1) {
        int mid = int((pass + fail) / 2);
        if (i - query(mid, i) <= A[i])
          pass = mid;
        else
          fail = mid;
      }
      upd(pass);
    }
    for (auto id : byR[i]) {
      ans[id] = query(L[id] - 1, i);
    }
  }
  for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
