#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const int MOD = (int)1e9 + 7;
const int MAX = (int)1e5 + 5;
const int MAXN = 460;
int stat[MAXN][MAX], a[MAX], cnt[MAX];
vector<int> cand;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    if (a[i] <= n) cnt[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i] >= i) cand.push_back(i);
  }
  for (int(i) = 0; (i) < (int)(((int)(cand).size())); (i)++) {
    for (int j = 1; j <= n; j++) {
      if (cand[i] == a[j])
        stat[i][j] = stat[i][j - 1] + 1;
      else
        stat[i][j] = stat[i][j - 1];
    }
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ans = 0;
    for (int(i) = 0; (i) < (int)(((int)(cand).size())); (i)++) {
      if (stat[i][r] - stat[i][l - 1] == cand[i]) ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
