#include <bits/stdc++.h>
using namespace std;
long long n, t, Ans, Res[200100], Z, Cnt[1000100], a[200200];
struct Query {
  int L, R, idx;
  Query() { L = R = idx = 0; }
  bool operator<(const Query &tmp) const {
    if (L / Z != tmp.L / Z) return L < tmp.L;
    return R < tmp.R;
  }
} q[200200];
void Add(int i) {
  if (a[i] > n) return;
  Ans -= (Cnt[a[i]] == a[i]);
  Cnt[a[i]]++;
  Ans += (Cnt[a[i]] == a[i]);
}
void Remove(int i) {
  if (a[i] > n) return;
  Ans -= (Cnt[a[i]] == a[i]);
  Cnt[a[i]]--;
  Ans += (Cnt[a[i]] == a[i]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> t;
  Z = sqrt(n) + 1;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= t; i++) {
    cin >> q[i].L >> q[i].R;
    q[i].idx = i;
  }
  sort(q + 1, q + t + 1);
  int Left = 1, Right = 0;
  for (int i = 1; i <= t; i++) {
    while (Right < q[i].R) {
      Right++;
      Add(Right);
    }
    while (Right > q[i].R) {
      Remove(Right);
      Right--;
    }
    while (Left < q[i].L) {
      Remove(Left);
      Left++;
    }
    while (Left > q[i].L) {
      Left--;
      Add(Left);
    }
    Res[q[i].idx] = Ans;
  }
  for (int i = 1; i <= t; i++) cout << Res[i] << '\n';
  return 0;
}
