#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 2;
const long long MOD = 998244353;
vector<int> id[MAX];
int cnt[MAX];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (x < MAX) {
      id[x].push_back(i);
      cnt[x]++;
    }
  }
  vector<int> v;
  for (int i = 1; i < MAX; i++) {
    if (cnt[i] >= i) v.push_back(i);
  }
  while (m--) {
    int l, r, res = 0;
    cin >> l >> r;
    for (int x : v) {
      int hi = upper_bound(id[x].begin(), id[x].end(), r) - id[x].begin();
      int lo = upper_bound(id[x].begin(), id[x].end(), l - 1) - id[x].begin();
      if (hi - lo == x) res++;
    }
    cout << res << "\n";
  }
}
