#include <bits/stdc++.h>
using namespace std;
vector<int> del[300007];
vector<pair<int, int>> vp;
vector<pair<int, int>> qr[300007];
int tree[600007];
int ans[300007];
inline int lowbit(int i) { return i & (-i); }
inline void ad(int i) {
  while (i <= 6e5) {
    tree[i]++;
    i += lowbit(i);
  }
}
inline void dl(int i) {
  while (i <= 6e5) {
    tree[i]--;
    i += lowbit(i);
  }
}
inline int query(int i) {
  int sum = 0;
  while (i > 0) {
    sum += tree[i];
    i -= lowbit(i);
  }
  return sum;
}
inline bool judge(int i, int j, int num) { return num - query(i) >= j; }
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  int num = 0;
  for (int i = 1; i != n + 1; i++) {
    int t;
    scanf("%d", &t);
    if (t <= i && t >= i - num) {
      int use = -1, l = 0, r = t;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (judge(mid - 1, i - t, num)) {
          use = max(mid, use);
          l = mid + 1;
        } else
          r = mid - 1;
      }
      ad(use);
      vp.emplace_back(use, i);
      num++;
    }
  }
  memset(tree, 0, sizeof(tree));
  for (auto p : vp) {
    del[p.first].push_back(p.second);
    ad(p.second);
  }
  for (int i = 0; i != q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    qr[a + 1].emplace_back(n - b, i);
  }
  for (int i = 1; i != n + 1; i++) {
    for (auto p : qr[i]) ans[p.second] = query(p.first);
    for (auto p : del[i]) dl(p);
  }
  for (int i = 0; i != q; i++) printf("%d\n", ans[i]);
  return 0;
}
