#include <bits/stdc++.h>
using namespace std;
const int Mxn = 100005;
const int bls = 320;
int ans = 0;
vector<int> cnt(Mxn);
struct que {
  int l, r, id;
} q[Mxn];
bool cmp(que a, que b) {
  if (a.l == b.l) return a.r < b.r;
  return a.l < b.l;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int v[n], ans1[m];
  for (long long i = 0; i < n; i++) cin >> v[i];
  for (long long i = 0; i < m; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
    q[i].l--, q[i].r--;
  }
  sort(q, q + m, cmp);
  long long mol = 0, mor = -1;
  if (n == 1) {
    if (v[0] == 1) {
      for (int i = 0; i < m; i++) cout << 1 << endl;
    } else {
      for (int i = 0; i < m; i++) cout << 0 << endl;
    }
  } else {
    for (int i = 0; i < m; i++) {
      long long x = q[i].l, y = q[i].r;
      while (mor < y) {
        mor++;
        cnt[v[mor]]++;
        if (cnt[v[mor]] == v[mor])
          ans++;
        else if (cnt[v[mor]] - 1 == v[mor])
          ans--;
      }
      while (mor > y) {
        cnt[v[mor]]--;
        if (cnt[v[mor]] == v[mor])
          ans++;
        else if (cnt[v[mor]] + 1 == v[mor])
          ans--;
        mor--;
      }
      while (mol < x) {
        cnt[v[mol]]--;
        if (cnt[v[mol]] == v[mol])
          ans++;
        else if (cnt[v[mol]] + 1 == v[mol])
          ans--;
        mol++;
      }
      while (mol > x) {
        mol--;
        cnt[v[mol]]++;
        if (cnt[v[mol]] == v[mol])
          ans++;
        else if (cnt[v[mol]] - 1 == v[mol])
          ans--;
      }
      ans1[q[i].id] = ans;
    }
    for (int i = 0; i < m; i++) {
      cout << ans1[i] << endl;
    }
  }
  return 0;
}
