#include <bits/stdc++.h>
using namespace std;
int d[501][100001];
int main() {
  int i, j, n, m, a[100001], l, r;
  map<int, int> num;
  int pos[501], le = 0;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    num[a[i]]++;
  }
  for (map<int, int>::iterator it = num.begin(); it != num.end(); it++) {
    if (it->second >= it->first) pos[++le] = it->first;
  }
  for (i = 1; i < le; i++) d[i][0] = 0;
  for (i = 1; i <= le; i++)
    for (j = 1; j <= n; j++)
      if (pos[i] == a[j])
        d[i][j] = d[i][j - 1] + 1;
      else
        d[i][j] = d[i][j - 1];
  for (i = 1; i <= m; i++) {
    cin >> l >> r;
    int ans = 0;
    for (j = 1; j <= le; j++) {
      if (d[j][r] - d[j][l - 1] == pos[j]) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
