#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 1e5 + 10;
int a[N], cnt[N];
struct node {
  int first, second, id;
  node() {}
  node(int _x, int _y, int _id) : first(_x), second(_y), id(_id) {}
} p[N];
bool cmp(node a, node b) {
  return a.second < b.second || (b.second == a.second && a.first < b.first);
}
vector<int> q[N];
int t[N], ans[N], n, id[N];
void update1(int first, int d) {
  while (first <= n) t[first] += d, first += first & -first;
}
void update(int first, int second, int d) {
  if (first > second) return;
  update1(first, d);
  update1(second + 1, -d);
}
int query1(int first) {
  int ans = 0;
  while (first > 0) {
    ans += t[first];
    first -= first & -first;
  }
  return ans;
}
int main() {
  int m, i, j, k;
  while (~scanf("%d%d", &n, &m)) {
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (i = 0; i < m; i++) {
      scanf("%d%d", &j, &k);
      p[i] = node(j, k, i);
    }
    sort(p, p + m, cmp);
    memset(cnt, 0, sizeof(cnt));
    memset(t, 0, sizeof(t));
    vector<int> v;
    v.clear();
    for (i = 1; i <= n; i++) {
      if (a[i] <= n) v.push_back(a[i]);
    }
    sort(v.begin(), v.end());
    ;
    v.erase(unique(v.begin(), v.end()), v.end());
    for (i = 0; i < v.size(); i++) {
      j = v[i];
      id[j] = i;
      q[i].clear();
      q[i].push_back(0);
    }
    int s = 0;
    for (i = 1; i <= n; i++) {
      if (a[i] <= n) {
        ++cnt[a[i]];
        j = id[a[i]];
        q[j].push_back(i);
        if (cnt[a[i]] >= a[i]) {
          k = cnt[a[i]] - a[i];
          if (k == 0)
            update(1, q[j][0], -1), update(q[j][0] + 1, q[j][1], 1);
          else
            update(q[j][k - 1] + 1, q[j][k], -1),
                update(q[j][k] + 1, q[j][k + 1], 1);
        }
      }
      while (s < m && p[s].second == i) {
        ans[p[s].id] = query1(p[s].first);
        s++;
      }
    }
    for (i = 0; i < m; i++) printf("%d\n", ans[i]);
  }
  return 0;
}
