#include <bits/stdc++.h>
int n, m, b[105000], a[105000], part[500][105000], c[500], k;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i] > n) continue;
    a[b[i]]++;
  };
  for (int i = 1; i <= n; i++)
    if (a[i] >= i) c[k++] = i;
  for (int i = 0; i < k; i++)
    for (int j = 1; j <= n; j++) part[i][j] = part[i][j - 1] + (b[j] == c[i]);
  for (int i = 0; i < m; i++) {
    int l, r, answer = 0;
    scanf("%d%d", &l, &r);
    for (int j = 0; j < k; j++)
      if (part[j][r] - part[j][l - 1] == c[j]) ++answer;
    printf("%d\n", answer);
  };
};
