#include <bits/stdc++.h>
using namespace std;
const int MaxN = (int)1e5 + 5;
const int K = 333;
int a[MaxN], b[MaxN];
pair<pair<int, int>, int> Q[MaxN];
int answer[MaxN];
inline bool mo_comp(const pair<pair<int, int>, int> &x,
                    const pair<pair<int, int>, int> &y) {
  int block_x = x.first.first / K;
  int block_y = y.first.first / K;
  if (block_x != block_y) return block_x < block_y;
  return y.first.second < x.first.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (a[i] > n) b[i] = -1;
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    Q[i].first.first = --x;
    Q[i].first.second = --y;
    Q[i].second = i;
  }
  sort(Q, Q + q, mo_comp);
  int mo_left = 1, mo_right = 0;
  int ans = 0;
  for (int i = 0; i < q; i++) {
    int left = Q[i].first.first;
    int right = Q[i].first.second;
    while (mo_right < right) {
      mo_right++;
      if (a[mo_right] <= MaxN) {
        b[a[mo_right]]++;
        if (b[a[mo_right]] == a[mo_right]) ans++;
        if (b[a[mo_right]] - a[mo_right] == 1) ans--;
      }
    }
    while (mo_right > right) {
      if (a[mo_right] <= MaxN) {
        b[a[mo_right]]--;
        if (b[a[mo_right]] == a[mo_right]) ans++;
        if (b[a[mo_right]] - a[mo_right] == -1) ans--;
      }
      mo_right--;
    }
    while (mo_left < left) {
      if (a[mo_left] <= MaxN) {
        b[a[mo_left]]--;
        if (b[a[mo_left]] == a[mo_left]) ans++;
        if (b[a[mo_left]] - a[mo_left] == -1) ans--;
      }
      mo_left++;
    }
    while (mo_left > left) {
      mo_left--;
      if (a[mo_left] <= MaxN) {
        b[a[mo_left]]++;
        if (b[a[mo_left]] == a[mo_left]) ans++;
        if (b[a[mo_left]] - a[mo_left] == 1) ans--;
      }
    }
    answer[Q[i].second] = ans;
  }
  for (int i = 0; i < q; i++) cout << answer[i] << "\n";
  return 0;
}
