#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Query;
  scanf("%d%d", &N, &Query);
  static int A[100000], Count[100001];
  memset(Count, 0, sizeof(Count));
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    if (A[i] <= N) Count[A[i]]++;
  }
  int M = 0;
  static int All[100000];
  for (int i = 1; i <= N; i++)
    if (Count[i] >= i) All[M++] = i;
  static int Sum[446][100001];
  for (int i = 0; i < M; i++)
    for (int j = Sum[i][0] = 0; j < N; j++)
      Sum[i][j + 1] = Sum[i][j] + (A[j] == All[i]);
  while (Query--) {
    int L, R, Ans = 0;
    scanf("%d%d", &L, &R);
    for (int i = 0; i < M; i++)
      if (Sum[i][R] - Sum[i][L - 1] == All[i]) Ans++;
    printf("%d\n", Ans);
  }
  return 0;
}
