#include <bits/stdc++.h>
using namespace std;
int tot[450][100005], a[100005], con[100005], inv[450];
int main() {
  int n, m, low, high, t = 0, cnt;
  memset(con, 0, sizeof(con));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (a[i] <= n) ++con[a[i]];
  }
  for (int i = 1; i <= n; ++i) {
    if (con[i] >= i) {
      inv[t] = i;
      tot[t++][0] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < t; ++j) {
      tot[j][i] = tot[j][i - 1];
      if (inv[j] == a[i]) ++tot[j][i];
    }
  }
  while (m--) {
    scanf("%d%d", &low, &high);
    cnt = 0;
    for (int i = 0; i < t; ++i) {
      if (tot[i][high] - tot[i][low - 1] == inv[i]) ++cnt;
    }
    cout << (cnt) << endl;
  }
  return 0;
}
