#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int block, cnt[maxn + 5], arr[maxn + 5], L, R, res[maxn + 5], n, m, tot;
struct query {
  bool operator<(const query& rhs) const {
    return (L / block) == (rhs.L / block) ? R < rhs.R : L < rhs.L;
  }
  int L = 0, R = 0, id = 0;
} q[maxn + 5];
inline void Add(int index) {
  int v = arr[index];
  if (v > n) return;
  if (cnt[v] == v) --tot;
  ++cnt[v];
  if (cnt[v] == v) ++tot;
}
inline void Del(int index) {
  int v = arr[index];
  if (v > n) return;
  if (cnt[v] == v) --tot;
  --cnt[v];
  if (cnt[v] == v) ++tot;
}
int main(void) {
  scanf("%d %d", &n, &m);
  block = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]);
  for (int i = 1; i <= m; ++i) {
    scanf("%d %d", &q[i].L, &q[i].R);
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m);
  L = 1;
  for (int i = 1; i <= m; ++i) {
    while (R < q[i].R) Add(++R);
    while (R > q[i].R) Del(R--);
    while (L < q[i].L) Del(L++);
    while (L > q[i].L) Add(--L);
    res[q[i].id] = tot;
  }
  for (int i = 1; i <= m; ++i) printf("%d\n", res[i]);
  return 0;
}
