#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
using namespace std;
int ans = 0;
pair<pair<int, int>, int> p[200000];
int a[200000];
int res[200000];
unordered_map<int, int> cnt;
int k;
bool comp(pair<pair<int, int>, int> &p, pair<pair<int, int>, int> &p1) {
  if ((p.first.first / k) != (p1.first.first / k))
    return ((p.first.first) < (p1.first.first));
  else
    return ((p.first.second) < (p1.first.second));
}
void add(int &p) {
  int x = a[p];
  if (cnt[x] == x) --ans;
  ++cnt[x];
  if (cnt[x] == x) ++ans;
}
void rem(int &p) {
  int x = a[p];
  if (cnt[x] == x) --ans;
  --cnt[x];
  if (cnt[x] == x) ++ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> a[i];
  }
  k = int(sqrt(n));
  for (int i = 0; i < m; ++i) {
    cin >> p[i].first.first >> p[i].first.second;
    --p[i].first.first;
    --p[i].first.second;
    p[i].second = i;
  }
  sort(p + 0, p + m, comp);
  int cur_l = p[0].first.first;
  int cur_r = p[0].first.second;
  for (int i = cur_l; i <= cur_r; ++i) {
    add(i);
  }
  res[p[0].second] = ans;
  for (int i = 1; i < m; ++i) {
    int l = p[i].first.first, r = p[i].first.second;
    while (cur_l < l) {
      rem(cur_l);
      ++cur_l;
    }
    while (cur_r < r) {
      ++cur_r;
      add(cur_r);
    }
    while (cur_l > l) {
      --cur_l;
      add(cur_l);
    }
    while (cur_r > r) {
      rem(cur_r);
      --cur_r;
    }
    res[p[i].second] = ans;
  }
  for (int i = 0; i < m; ++i) {
    cout << res[i] << '\n';
  }
}
