#include <bits/stdc++.h>
using namespace std;
const int INP_SIZE = 100007;
const int QUERIES_SIZE = 100007;
const int SQRTN = 320;
struct query {
  int l, r, q_idx, block_idx;
  query() {}
  query(int _l, int _r, int _q_idx) {
    l = _l - 1, r = _r - 1, q_idx = _q_idx, block_idx = _l / SQRTN;
  }
  bool operator<(const query &y) const {
    if (block_idx != y.block_idx) return block_idx < y.block_idx;
    return r < y.r;
  }
};
int n, m;
int inp[INP_SIZE], result = 0;
int q_ans[QUERIES_SIZE];
query queries[QUERIES_SIZE];
int cnt[1000000 + 9];
void add(int idx) {
  if (inp[idx] > 1e5) return;
  cnt[inp[idx]]++;
  if (cnt[inp[idx]] == inp[idx] + 1)
    result--;
  else if (cnt[inp[idx]] == inp[idx])
    result++;
}
void remove(int idx) {
  if (inp[idx] > 1e5) return;
  cnt[inp[idx]]--;
  if (cnt[inp[idx]] == inp[idx]) result++;
  if (cnt[inp[idx]] == inp[idx] - 1) result--;
}
void process() {
  sort(queries, queries + m);
  int curL = 1, curR = 0;
  for (int i = 0; i < m; i++) {
    while (curL < queries[i].l) remove(curL++);
    while (curL > queries[i].l) add(--curL);
    while (curR < queries[i].r) add(++curR);
    while (curR > queries[i].r) remove(curR--);
    q_ans[queries[i].q_idx] = result;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &inp[i]);
  for (int i = 0; i < m; i++) {
    int left, right;
    scanf("%d%d", &left, &right);
    queries[i] = query(left, right, i);
  }
  process();
  for (int i = 0; i < m; i++) printf("%d\n", q_ans[i]);
}
