#include <bits/stdc++.h>
using namespace std;
int n, A[100001], q, tmp[100001], l, r, Q[100001][2], ans[100001];
vector<int> v;
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &tmp[i]);
    if (tmp[i] < 100001) A[tmp[i]]++;
  }
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &Q[i][0], &Q[i][1]);
  }
  for (int i = 1; i <= n; i++) {
    if (A[i] < i) continue;
    int PS[n + 1];
    PS[0] = 0;
    for (int j = 1; j <= n; j++) {
      PS[j] = PS[j - 1] + (tmp[j] == i);
    }
    for (int j = 0; j < q; j++) {
      ans[j] += (PS[Q[j][1]] - PS[Q[j][0] - 1]) == i;
    }
  }
  for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
}
