#include <bits/stdc++.h>
using namespace std;
int mult(int a, int b, int p = 1000000007) { return ((a % p) * (b % p)) % p; }
int add(int a, int b, int p = 1000000007) { return (a % p + b % p) % p; }
int fpow(int n, int k, int p = 1000000007) {
  int r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
int inv(int a, int p = 1000000007) { return fpow(a, p - 2, p); }
int v[100003];
unordered_map<int, vector<int> > vv;
int val[100003];
int a[100003];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] <= n) v[a[i]]++;
  }
  int cnt = 0;
  for (int i = 1; i < n + 1; i++) {
    if (v[i] >= i) {
      val[cnt] = i;
      vv[cnt].push_back(0);
      if (i == a[0]) {
        vv[cnt].push_back(1);
      } else
        vv[cnt].push_back(0);
      for (int j = 1; j < n; j++) {
        if (a[j] == i) {
          vv[cnt].push_back(vv[cnt][j] + 1);
        } else
          vv[cnt].push_back(vv[cnt][j]);
      }
      cnt++;
    }
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    int ans = 0;
    for (int i = 0; i < cnt; i++) {
      if (vv[i][r] - vv[i][l - 1] == val[i]) {
        ans++;
      }
    }
    cout << ans << "\n";
  }
}
