#include <bits/stdc++.h>
using namespace std;
int n, m, a[100009], c[100009];
int b[500], p;
int s[500][100009];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (a[i] <= n && ++c[a[i]] == a[i]) b[p++] = a[i];
  }
  for (int i = 0; i < p; i++)
    for (int j = 1; j <= n; j++) s[i][j] = s[i][j - 1] + (a[j] == b[i]);
  for (int i = 0, l, r; i < m; i++) {
    scanf("%d%d", &l, &r);
    int res = 0;
    for (int i = 0; i < p; i++)
      if (s[i][r] - s[i][l - 1] == b[i]) res++;
    printf("%d\n", res);
  }
}
