#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
pair<int, int> p[N], tp[N];
map<pair<int, int>, int> ma;
int k;
unordered_map<int, int> fre;
bool cmp(pair<int, int> a, pair<int, int> b) {
  int b1 = a.first / k, b2 = b.first / k;
  if (b1 != b2) return b1 < b2;
  return a.second < b.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, i;
  cin >> n >> m;
  int a[n + 10];
  for (i = 0; i <= n - 1; i++) cin >> a[i];
  k = sqrt((long double)n);
  for (i = 0; i <= m - 1; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i] = make_pair(l, r);
    tp[i] = p[i];
  }
  sort(p, p + m, cmp);
  int ans = 0, res[m + 10];
  int rptr = 0, lptr = 0;
  fre[a[0]]++;
  if (a[0] == 1) ans++;
  for (i = 0; i <= m - 1; i++) {
    int lt = p[i].first;
    int rt = p[i].second;
    while (rptr < rt) {
      rptr++;
      if (fre[a[rptr]] == a[rptr]) ans--;
      fre[a[rptr]]++;
      if (fre[a[rptr]] == a[rptr]) ans++;
    }
    while (lptr < lt) {
      if (fre[a[lptr]] == a[lptr]) ans--;
      fre[a[lptr]]--;
      if (fre[a[lptr]] == a[lptr]) ans++;
      lptr++;
    }
    while (rptr > rt) {
      if (fre[a[rptr]] == a[rptr]) ans--;
      fre[a[rptr]]--;
      if (fre[a[rptr]] == a[rptr]) ans++;
      rptr--;
    }
    while (lptr > lt) {
      lptr--;
      if (fre[a[lptr]] == a[lptr]) ans--;
      fre[a[lptr]]++;
      if (fre[a[lptr]] == a[lptr]) ans++;
    }
    ma[p[i]] = ans;
  }
  for (i = 0; i <= m - 1; i++) cout << ma[tp[i]] << "\n";
  return 0;
}
