#include <bits/stdc++.h>
using namespace std;
const double eps = 0.000000000000001;
int n, m, sqn;
int A[100010];
int ans[100010];
int hs[100010];
struct qss {
  int l, r, ind;
};
qss que[100010];
int mas = 0;
void yolo(int ind, int val) {
  if (ind > n) return;
  if (hs[ind] == ind) mas--;
  hs[ind] += val;
  if (hs[ind] == ind) mas++;
}
bool cmp(qss a, qss b) {
  if (a.l / sqn == b.l / sqn) return a.r < b.r;
  return a.l < b.l;
}
int main() {
  scanf("%d%d", &n, &m);
  int i, j, k;
  for (i = 1; i <= n; i++) scanf("%d", &A[i]);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &que[i].l, &que[i].r);
    que[i].ind = i;
  }
  sqn = sqrtl(n) * 1.25;
  sort(que, que + m, cmp);
  int l, r;
  l = 1;
  r = 1;
  mas = 0;
  yolo(A[1], 1);
  for (i = 0; i < m; i++) {
    int lx = que[i].l, rx = que[i].r;
    while (lx < l) {
      l--;
      yolo(A[l], 1);
    }
    while (r < rx) {
      r++;
      yolo(A[r], 1);
    }
    while (lx > l) {
      yolo(A[l], -1);
      l++;
    }
    while (r > rx) {
      yolo(A[r], -1);
      r--;
    }
    ans[que[i].ind] = mas;
  }
  for (i = 0; i < m; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
