#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 100;
int magic;
int ANS;
unordered_map<int, int> cnt;
vector<pair<int, pair<int, int> > > q;
int RES[maxn];
int a[maxn];
bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  if (a.second.first / magic == b.second.first / magic) {
    return a.second.second < b.second.second;
  } else
    return a.second.first / magic < b.second.first / magic;
}
void add(int pos) {
  if (cnt[a[pos]] == a[pos]) ANS--;
  cnt[a[pos]]++;
  if (cnt[a[pos]] == a[pos]) ANS++;
}
void del(int pos) {
  if (cnt[a[pos]] == a[pos]) ANS--;
  cnt[a[pos]]--;
  if (cnt[a[pos]] == a[pos]) ANS++;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  magic = sqrt(n) + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    q.push_back(make_pair(i, make_pair(l, r)));
  }
  sort(q.begin(), q.end(), &cmp);
  int L = 1, R = 1;
  add(1);
  for (int i = 0; i < q.size(); i++) {
    int l = q[i].second.first;
    int r = q[i].second.second;
    while (L < l) {
      del(L++);
    }
    while (L > l) {
      add(--L);
    }
    while (R < r) {
      add(++R);
    }
    while (R > r) {
      del(R--);
    }
    RES[q[i].first] = ANS;
  }
  for (int i = 1; i <= m; i++) {
    printf("%d\n", RES[i]);
  }
  return 0;
}
