#include <bits/stdc++.h>
using namespace std;
const int N = 1000 * 100 + 10;
int a[N], p[N], num[N], rnum[N], ps[N][500];
int main() {
  int n, m, pt = 1, l, r;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] <= n) p[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] >= i) {
      num[i] = pt;
      rnum[pt++] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < pt; j++) ps[i][j] = ps[i - 1][j];
    if (a[i] <= n && num[a[i]]) ps[i][num[a[i]]]++;
  }
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    l--;
    int ans = 0;
    for (int j = 1; j < pt; j++)
      if (ps[r][j] - ps[l][j] == rnum[j]) ans++;
    cout << ans << endl;
  }
  return 0;
}
