#include <bits/stdc++.h>
using namespace std;
const int N = 222222;
const int M = 1111111;
int pos[N], w[N], key[N], ord[N], check[M], res[N];
int tot, n, m, bi;
int a[N], l[N], r[N];
void insert(int x) {
  check[key[x]]++;
  if (check[key[x]] == w[key[x]]) ++tot;
  if (check[key[x]] - 1 == w[key[x]]) --tot;
}
void erase(int x) {
  check[key[x]]--;
  if (check[key[x]] == w[key[x]]) ++tot;
  if (check[key[x]] + 1 == w[key[x]]) --tot;
}
bool cmp(const int &x, const int &y) {
  if (l[x] / bi == l[y] / bi) return r[x] > r[y];
  return l[x] < l[y];
}
bool cmp2(const int &x, const int &y) { return a[x] < a[y]; }
int main() {
  scanf("%d%d", &n, &m);
  int i;
  for (i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    pos[i] = i;
  }
  sort(pos + 1, pos + n + 1, cmp2);
  int j = 0;
  for (i = 1; i <= n; ++i) {
    j += (a[pos[i]] != a[pos[i - 1]]);
    w[j] = a[pos[i]];
    key[pos[i]] = j;
  }
  for (i = 1; i <= m; ++i) scanf("%d%d", &l[i], &r[i]);
  for (i = 1; i <= m; ++i) ord[i] = i;
  bi = (int)sqrt(n * 1.0);
  sort(ord + 1, ord + m + 1, cmp);
  int al = 1, ar = 1;
  memset((check), 0, sizeof(check));
  tot = 0;
  insert(1);
  for (i = 1; i <= m; ++i) {
    while (ar < r[ord[i]]) insert(++ar);
    while (al < l[ord[i]]) erase(al++);
    while (al > l[ord[i]]) insert(--al);
    while (ar > r[ord[i]]) erase(ar--);
    res[ord[i]] = tot;
  }
  for (i = 1; i <= m; ++i) printf("%d\n", res[i]);
  return 0;
}
