#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 5;
unordered_map<int, int> mp;
int block, ans[mx], n, m;
struct query {
  int l, r, id;
};
bool comp(query x, query y) {
  if (x.l / block != y.l / block) return x.l / block < y.l / block;
  return x.r < y.r;
}
void find_ans(int a[], query q[]) {
  block = (int)sqrt(n);
  sort(q, q + m, comp);
  int curl = 0, curr = 0, cursum = 0;
  for (int i = 0; i < m; ++i) {
    int l = q[i].l, r = q[i].r;
    while (curl < l) {
      if (mp[a[curl]] == a[curl]) --cursum;
      --mp[a[curl]];
      if (mp[a[curl]] == a[curl]) ++cursum;
      ++curl;
    }
    while (curl > l) {
      --curl;
      if (mp[a[curl]] == a[curl]) --cursum;
      ++mp[a[curl]];
      if (mp[a[curl]] == a[curl]) ++cursum;
    }
    while (curr <= r) {
      if (mp[a[curr]] == a[curr]) --cursum;
      ++mp[a[curr]];
      if (mp[a[curr]] == a[curr]) ++cursum;
      ++curr;
    }
    while (curr > (r + 1)) {
      --curr;
      if (mp[a[curr]] == a[curr]) --cursum;
      --mp[a[curr]];
      if (mp[a[curr]] == a[curr]) ++cursum;
    }
    ans[q[i].id] = cursum;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int a[n];
  query q[m];
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < m; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    q[i].l = l - 1, q[i].r = r - 1, q[i].id = i;
  }
  find_ans(a, q);
  for (int i = 0; i < m; ++i) printf("%d\n", ans[i]);
  return 0;
}
