#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int n, m;
vector<int> pos[MAXN + 10];
vector<int> T;
int main() {
  int i, j, x, y, cnt, p, t;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x <= n) pos[x].push_back(i);
  }
  for (i = 1; i <= n; i++)
    if (pos[i].size() >= i) T.push_back(i);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    cnt = 0;
    for (j = 0; j < T.size(); j++) {
      p = T[j];
      t = (upper_bound(pos[p].begin(), pos[p].end(), y) - pos[p].begin()) -
          (lower_bound(pos[p].begin(), pos[p].end(), x) - pos[p].begin());
      if (t == p) cnt++;
    }
    printf("%d\n", cnt);
  }
  return 0;
}
