#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, second, a[N];
struct query {
  int l, r, p, block;
} t[N];
int res[N], curL, curR, curres = 0, cnt[N];
bool cmp(query a, query b) {
  return ((a.block < b.block) || (a.block == b.block && a.r < b.r));
}
void add(int pos) {
  if (a[pos] > n) return;
  cnt[a[pos]]++;
  if (cnt[a[pos]] == a[pos]) curres++;
  if (cnt[a[pos]] == a[pos] + 1) curres--;
}
void rem(int pos) {
  if (a[pos] > n) return;
  cnt[a[pos]]--;
  if (cnt[a[pos]] == a[pos]) curres++;
  if (cnt[a[pos]] == a[pos] - 1) curres--;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  second = sqrt(n);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> t[i].l >> t[i].r;
    t[i].p = i;
    t[i].block = t[i].l / second;
  }
  sort(t + 1, t + 1 + m, cmp);
  add(++curL);
  curR = 1;
  for (int i = 1; i <= m; i++) {
    while (curL < t[i].l) rem(curL++);
    while (curL > t[i].l) add(--curL);
    while (curR > t[i].r) rem(curR--);
    while (curR < t[i].r) add(++curR);
    res[t[i].p] = curres;
  }
  for (int i = 1; i <= m; i++) cout << res[i] << "\n";
  return 0;
}
