#include <bits/stdc++.h>
using namespace std;
int n, q, a[300005], l[300005], r[300005], p[300005], c[300005], las[300005],
    f[300005], ans[300005];
void add(int x) {
  if (!x) return;
  for (; x <= n; x += (x & -x)) c[x]++;
}
int quy(int x) {
  int res = 0;
  for (; x; x -= (x & -x)) res += c[x];
  return res;
}
int bs(int k) {
  if (k < 0) return 0;
  int l = 1, r = n, res = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (quy(n) - quy(mid - 1) >= k)
      l = mid + 1, res = mid;
    else
      r = mid - 1;
  }
  return res;
}
bool cmp(const int a, const int b) { return r[a] > r[b]; }
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= q; i++) scanf("%d%d", &l[i], &r[i]), p[i] = i;
  sort(p + 1, p + q + 1, cmp);
  r[0] = n;
  for (int i = 1; i <= q; i++) {
    for (int j = n - r[p[i - 1]] + 1; j <= n - r[p[i]]; j++)
      add(f[j] = a[j] == j ? j : bs(j - a[j]));
    ans[p[i]] = quy(n) - quy(l[p[i]]);
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
