#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) >= 1) {
    vector<int> as(n);
    for (int i = 0; i < n; i++) scanf("%d", &as[i]);
    vector<int> vals = as;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    vector<int> cnt(((int)(vals).size()));
    for (int i = 0; i < n; i++) {
      as[i] = lower_bound(vals.begin(), vals.end(), as[i]) - vals.begin();
      cnt[as[i]]++;
    }
    vector<int> good;
    for (int i = 0; i < ((int)(vals).size()); i++)
      if (cnt[i] >= vals[i]) good.push_back(i);
    vector<vector<int> > cnts(((int)(good).size()), vector<int>(n + 1, 0));
    for (int i = 0; i < ((int)(good).size()); i++) {
      for (int i2 = 0; i2 < n; i2++)
        cnts[i][i2 + 1] = cnts[i][i2] + (as[i2] == good[i]);
    }
    for (int step = 0; step < m; step++) {
      int l, r;
      scanf("%d%d", &l, &r), l--, r--;
      int ans = 0;
      for (int i = 0; i < ((int)(good).size()); i++) {
        int cnt = cnts[i][r + 1] - cnts[i][l];
        ans += cnt == vals[good[i]];
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
