#include <bits/stdc++.h>
using namespace std;
int n, m, a[100001], l, r, BLOCK_SIZE, res[100001], curL = 1, curR = 1, ans = 0,
                                                    f[100001];
vector<pair<pair<int, int>, int> > queries(100001);
bool mo_cmp(const pair<pair<int, int>, int> &a,
            const pair<pair<int, int>, int> &b) {
  if (a.first.first / BLOCK_SIZE != b.first.first / BLOCK_SIZE)
    return a.first.first < b.first.first;
  return a.first.second < b.first.second;
}
void add(int pos) {
  if (a[pos] <= 100000) {
    if (f[a[pos]] == a[pos]) ans--;
    f[a[pos]]++;
    if (f[a[pos]] == a[pos]) ans++;
  }
}
void del(int pos) {
  if (a[pos] <= 100000) {
    if (f[a[pos]] == a[pos]) ans--;
    f[a[pos]]--;
    if (f[a[pos]] == a[pos]) ans++;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  BLOCK_SIZE = sqrt(n);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    queries[i] = {{l, r}, i};
  }
  sort(queries.begin(), queries.begin() + m, mo_cmp);
  for (int i = 0; i < m; i++) {
    l = queries[i].first.first;
    r = queries[i].first.second;
    while (curL < l) del(curL++);
    while (curL > l) add(--curL);
    while (curR <= r) add(curR++);
    while (curR > r + 1) del(--curR);
    res[queries[i].second] = ans;
  }
  for (int i = 0; i < m; i++) cout << res[i] << endl;
  return 0;
}
