#include <bits/stdc++.h>
using namespace std;
int a[100005], c[100005], mx, sz, an[100005];
vector<pair<pair<int, int>, int> > v;
map<int, int> cn;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / sz == b.first.first / sz)
    return a.first.second < b.first.second;
  return a.first.first / sz < b.first.first / sz;
}
int main() {
  int n, q, l, r;
  scanf("%d %d", &n, &q);
  sz = sqrt(n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > n) {
      a[i] = 0;
    } else {
      c[a[i]]++;
      mx = max(a[i], mx);
    }
  }
  for (int i = 1; i <= mx; i++) {
    if (i > c[i]) c[i] = 0;
  }
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &l, &r);
    v.push_back(make_pair(make_pair(l, r), i + 1));
  }
  sort(v.begin(), v.end(), cmp);
  int ct = 0, i = 1, j = 1;
  for (int k = 0; k < q; k++) {
    l = v[k].first.first, r = v[k].first.second, ct = 0;
    while (j <= r) {
      if (c[a[j]]) cn[a[j]]++;
      j++;
    }
    while (j > r + 1) {
      j--;
      if (c[a[j]]) cn[a[j]]--;
    }
    while (i > l) {
      i--;
      if (c[a[i]]) cn[a[i]]++;
    }
    while (i < l) {
      if (c[a[i]]) cn[a[i]]--;
      i++;
    }
    map<int, int>::iterator it = cn.begin();
    for (; it != cn.end(); it++) {
      if (it->first == it->second) ct++;
    }
    an[v[k].second] = ct;
  }
  for (int i = 1; i <= q; i++) {
    printf("%d\n", an[i]);
  }
}
