#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
const int M = 1e5 + 1;
const int SQN = sqrt(N) + 1;
int n, q;
int arr[N];
int print[N];
unordered_map<int, int> cnt;
struct query {
  int l, r;
  int idx;
  int block;
  query() {}
  query(int _l, int _r, int _id) {
    l = _l;
    r = _r;
    idx = _id;
    block = _l / SQN;
  }
  bool operator<(const query &b) const {
    if (block != b.block) {
      return block < b.block;
    }
    return r < b.r;
  }
};
query Q[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    Q[i] = query(l, r, i);
  }
  sort(Q + 1, Q + 1 + q);
  int ans = 0;
  int currL = 1, currR = 0;
  for (int i = 1; i <= q; i++) {
    int l = Q[i].l;
    int r = Q[i].r;
    int id = Q[i].idx;
    while (currR < r) {
      ++currR;
      if (cnt[arr[currR]] == arr[currR]) ans--;
      ++cnt[arr[currR]];
      if (cnt[arr[currR]] == arr[currR]) ans++;
    }
    while (currL > l) {
      --currL;
      if (cnt[arr[currL]] == arr[currL]) ans--;
      ++cnt[arr[currL]];
      if (cnt[arr[currL]] == arr[currL]) ans++;
    }
    while (currR > r) {
      if (cnt[arr[currR]] == arr[currR]) ans--;
      --cnt[arr[currR]];
      if (cnt[arr[currR]] == arr[currR]) ans++;
      --currR;
    }
    while (currL < l) {
      if (cnt[arr[currL]] == arr[currL]) ans--;
      --cnt[arr[currL]];
      if (cnt[arr[currL]] == arr[currL]) ans++;
      ++currL;
    }
    print[id] = ans;
  }
  for (int i = 1; i <= q; i++) cout << print[i] << endl;
  return 0;
}
