#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 123;
int n, Q, block;
int cnt[N], a[N], ans[N];
struct st {
  int l, r, id;
};
vector<st> q;
bool cmp(st f, st s) {
  if (f.l / block != s.l / block) return f.l < s.l;
  return f.r < s.r;
}
int kol = 0;
void add(int pos) {
  pos = a[pos];
  if (pos <= n) {
    cnt[pos]++;
    if (cnt[pos] == pos) kol++;
    if (cnt[pos] == pos + 1) kol--;
  }
}
void del(int pos) {
  pos = a[pos];
  if (pos <= n) {
    cnt[pos]--;
    if (cnt[pos] == pos) kol++;
    if (cnt[pos] == pos - 1) kol--;
  }
}
int main() {
  cin >> n >> Q;
  block = sqrt(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= Q; i++) {
    int l, r;
    cin >> l >> r;
    q.push_back({l, r, i});
  }
  sort(q.begin(), q.end(), &cmp);
  int l = 1, r = 0;
  for (int i = 0; i < Q; i++) {
    while (r < q[i].r) add(++r);
    while (l < q[i].l) del(l++);
    while (r > q[i].r) del(r--);
    while (l > q[i].l) add(--l);
    ans[q[i].id] = kol;
  }
  for (int i = 1; i <= Q; i++) cout << ans[i] << endl;
  return 0;
}
