#include <bits/stdc++.h>
using namespace std;
int cnt[311111], a[311111], answer, ans[100005];
struct node {
  int L, R, i;
} q[311111];
bool cmp(node x, node y) {
  if (x.L / 555 != y.L / 555) {
    return x.L / 555 < y.L / 555;
  }
  return x.R < y.R;
}
void add(int position) {
  if (a[position] <= 100000) {
    if (cnt[a[position]] == a[position] - 1) {
      answer++;
    } else if (cnt[a[position]] == a[position]) {
      answer--;
    }
    cnt[a[position]]++;
  }
}
void remove(int position) {
  if (a[position] <= 100000) {
    if (cnt[a[position]] == a[position] + 1) {
      answer++;
    } else if (cnt[a[position]] == a[position]) {
      answer--;
    }
    cnt[a[position]]--;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int m;
  scanf("%d", &m);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &q[i].L, &q[i].R);
    q[i].L--;
    q[i].R--;
    q[i].i = i;
  }
  sort(q, q + m, cmp);
  int currentL = 0, currentR = 0;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR <= R) {
      add(currentR);
      currentR++;
    }
    while (currentR > R + 1) {
      remove(currentR - 1);
      currentR--;
    }
    ans[q[i].i] = answer;
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
}
