#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, m, k;
struct node {
  int l, r, id;
} Q[maxn];
int pos[maxn];
bool cmp(const node &a, const node &b) {
  return pos[a.l] < pos[b.l] ||
         (pos[a.l] == pos[b.l] && (pos[a.l] & 1 ? a.r < b.r : a.r > b.r));
}
long long num[maxn * 2];
long long a[maxn], b[maxn], c[maxn];
long long ans[maxn], ans2[maxn];
int L = 0, R = 0;
long long Ans = 0;
void add(int x) {
  num[c[x]]++;
  if (num[c[x]] == a[x])
    Ans++;
  else if (num[c[x]] == a[x] + 1)
    Ans--;
}
void del(int x) {
  num[c[x]]--;
  if (num[c[x]] == a[x])
    Ans++;
  else if (num[c[x]] + 1 == a[x])
    Ans--;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(num, 0, sizeof(num));
    int sz = sqrt(n * 1.0);
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      pos[i] = i / sz;
      b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    int num1 = unique(b + 1, b + n + 1) - b - 1;
    for (int i = 1; i <= n; i++)
      c[i] = lower_bound(b + 1, b + num1 + 1, a[i]) - b;
    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, cmp);
    num[0] = 0;
    L = 1;
    R = 0;
    Ans = 0;
    for (int i = 1; i <= m; i++) {
      while (R < Q[i].r) {
        R++;
        add(R);
      }
      while (L > Q[i].l) {
        L--;
        add(L);
      }
      while (L < Q[i].l) {
        del(L);
        L++;
      }
      while (R > Q[i].r) {
        del(R);
        R--;
      }
      ans[Q[i].id] = Ans;
    }
    for (int i = 1; i <= m; i++) printf("%I64d\n", ans[i]);
  }
  return 0;
}
