#include <bits/stdc++.h>
using namespace std;
int cnt[100005];
int gg[500];
int gn;
int ww[100005];
int cc[100005][455];
int n, m;
int dd[100005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &dd[i]);
    if (dd[i] <= n) {
      cnt[dd[i]]++;
      if (cnt[dd[i]] >= dd[i] && !ww[dd[i]]) {
        gg[++gn] = dd[i];
        ww[dd[i]] = gn;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    memcpy(cc[i], cc[i - 1], sizeof(cc[i - 1]));
    if (dd[i] <= n && ww[dd[i]]) {
      cc[i][ww[dd[i]]]++;
    }
  }
  int a, b;
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &a, &b);
    int res = 0;
    for (int j = 1; j <= gn; ++j) {
      if (cc[b][j] - cc[a - 1][j] == gg[j]) res++;
    }
    printf("%d\n", res);
  }
  return 0;
}
