#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
vector<int> s[maxn];
int occ[maxn];
int el[maxn];
int a[maxn];
int main() {
  int n;
  scanf("%d", &n);
  int m;
  scanf("%d", &m);
  int M = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
    if (x >= maxn) continue;
    occ[x]++;
    if (occ[x] == x) {
      M++;
      el[M] = occ[x];
      s[M].push_back(0);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= M; j++) {
      int ls = s[j][s[j].size() - 1];
      if (a[i] == el[j])
        s[j].push_back(ls + 1);
      else
        s[j].push_back(ls);
    }
  }
  while (m--) {
    int l, r;
    scanf("%d", &l);
    scanf("%d", &r);
    int ans = 0;
    for (int i = 1; i <= M; i++) {
      int oc = s[i][r] - s[i][l - 1];
      if (oc == el[i]) ans++;
    }
    printf("%d", ans);
    printf("\n");
  }
}
