#include <bits/stdc++.h>
using namespace std;
vector<int> can;
int mp[100011];
struct node {
  int l, r;
  int res;
} q[100011];
int num[100011];
int pre[100011];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(mp, 0, sizeof(mp));
    ;
    for (int i = 1; i <= n; i++) {
      int tt;
      scanf("%d", &tt);
      if (tt > n) continue;
      mp[tt]++;
      if (mp[tt] == tt) can.push_back(tt);
      num[i] = tt;
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &q[i].l, &q[i].r);
      q[i].res = 0;
    }
    for (int i = 0; i < can.size(); i++) {
      pre[0] = 0;
      for (int j = 1; j <= n; j++) pre[j] = pre[j - 1] + (num[j] == can[i]);
      for (int j = 1; j <= m; j++) {
        if (pre[q[j].r] - pre[q[j].l - 1] == can[i]) q[j].res++;
      }
    }
    for (int i = 1; i <= m; i++) printf("%d\n", q[i].res);
  }
  return 0;
}
