#include <bits/stdc++.h>
using namespace std;
int n, m, sqr;
int a[100005];
int cnt[100005];
int res[100005];
int ans = 0;
struct que {
  int l, r, idx;
} q[100005];
bool operator<(const que &q1, const que &q2) {
  if (q1.l / sqr != q2.l / sqr) return q1.l < q2.l;
  if ((q1.l / sqr) % 2) return q1.r > q2.r;
  return q1.r < q2.r;
}
int main() {
  memset(res, 0, sizeof(res));
  scanf("%d %d", &n, &m);
  sqr = sqrt(m);
  if (sqr * sqr != m) sqr++;
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &q[i].l, &q[i].r);
    q[i].l;
    q[i].r;
    q[i].idx = i;
  }
  sort(q, q + m);
  int l = 1, r = 0;
  for (int i = 0; i < m; i++) {
    while (q[i].r > r) {
      r++;
      if (a[r] <= 100000) {
        if (cnt[a[r]] == a[r]) ans--;
        cnt[a[r]]++;
        if (cnt[a[r]] == a[r]) ans++;
      }
    }
    while (q[i].l < l) {
      l--;
      if (a[l] <= 100000) {
        if (cnt[a[l]] == a[l]) ans--;
        cnt[a[l]]++;
        if (cnt[a[l]] == a[l]) ans++;
      }
    }
    while (q[i].r < r) {
      if (a[r] <= 100000) {
        if (cnt[a[r]] == a[r]) ans--;
        cnt[a[r]]--;
        if (cnt[a[r]] == a[r]) ans++;
      }
      r--;
    }
    while (q[i].l > l) {
      if (a[l] <= 100000) {
        if (cnt[a[l]] == a[l]) ans--;
        cnt[a[l]]--;
        if (cnt[a[l]] == a[l]) ans++;
      }
      l++;
    }
    res[q[i].idx] = ans;
  }
  for (int i = 0; i < m; i++) printf("%d\n", res[i]);
  return 0;
}
