#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, m, cnt;
int d[500][maxn];
int f[maxn], cn[maxn];
map<int, int> mp;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &f[i]);
    mp[f[i]]++;
  }
  cnt = 0;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
    if (it->first <= it->second) {
      cnt++;
      cn[cnt] = it->first;
    }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= cnt; j++)
      if (cn[j] == f[i])
        d[j][i + 1] = d[j][i] + 1;
      else
        d[j][i + 1] = d[j][i];
  }
  int x, y, ans, i;
  while (m--) {
    ans = 0;
    scanf("%d%d", &x, &y);
    for (i = 1; i <= y - x + 1 && i <= cnt; i++) {
      if (d[i][y] - d[i][x - 1] == cn[i]) ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
