#include <bits/stdc++.h>
using namespace std;
vector<long long> occ[100001];
vector<long long> check;
int main() {
  std::ios::sync_with_stdio(false);
  long long n, m, a, b;
  scanf("%lld %lld", &n, &m);
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    scanf("%lld", &a);
    if (a > 100000) continue;
    occ[a].push_back(i);
  }
  for (int i = 1; i <= 100000; i++) {
    if (occ[i].size() >= i) check.push_back(i);
  }
  long long k = check.size();
  for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
       i += 1 - 2 * ((0) > (m))) {
    scanf("%lld %lld", &a, &b);
    a--;
    b--;
    long long ans = 0;
    for (int j = 0; j < k; j++) {
      auto atual = check[j];
      auto& v = occ[atual];
      auto lb = lower_bound(v.begin(), v.end(), a);
      auto ub = upper_bound(v.begin(), v.end(), b);
      if (ub - lb == check[j]) ans++;
    }
    printf("%lld\n", ans);
  }
}
