#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int cnt[N];
int sum[500][N];
int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = (0), _b = (n); i < (_b); ++i) cin >> a[i];
  for (int i = (0), _b = (n); i < (_b); ++i)
    if (a[i] <= n) cnt[a[i]]++;
  vector<int> v;
  for (int i = (1), _b = (n + 1); i < (_b); ++i)
    if (cnt[i] >= i) v.push_back(i);
  for (int i = (0), _b = (v.size()); i < (_b); ++i)
    for (int j = (0), _b = (n); j < (_b); ++j)
      sum[i][j + 1] = sum[i][j] + ((a[j] == v[i]) ? 1 : 0);
  int l, r;
  while (m--) {
    cin >> l >> r;
    --l;
    int ans = 0;
    for (int i = (0), _b = (v.size()); i < (_b); ++i) {
      if (sum[i][r] - sum[i][l] == v[i]) ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}
