#include <bits/stdc++.h>
using namespace std;
vector<int> v(100005, 0), lnk(100005, 0), cnt(100005, 0);
int b[100005][450];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] <= n) cnt[v[i]]++;
  }
  vector<int> num(450, 0);
  long long int s = 0;
  for (long long int i = 1; i <= n; i++) {
    if (cnt[i] >= i) num[++s] = i, lnk[i] = s;
  }
  for (long long int i = 1; i <= n; i++) {
    memcpy(b[i], b[i - 1], sizeof(b[i]));
    if (v[i] <= n && lnk[v[i]]) b[i][lnk[v[i]]]++;
  }
  while (m--) {
    long long int l, r;
    cin >> l >> r;
    long long int ans = 0;
    for (long long int j = 1; j <= s; j++) {
      if (b[r][j] - b[l - 1][j] == num[j]) {
        ans++;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
