#include <bits/stdc++.h>
using namespace std;
int dp[446][100002];
int main() {
  int n, q;
  cin >> n >> q;
  int a[n + 12];
  for (int i = 1; i <= n; i++) cin >> a[i];
  int freq[n + 12];
  memset(freq, 0, sizeof(freq));
  for (int i = 1; i <= n; i++) {
    if (a[i] > n) continue;
    freq[a[i]]++;
  }
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    if (freq[i] >= i) v.push_back(i);
  }
  for (int i = 0; i < v.size(); i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i][j - 1] + (a[j] == v[i]);
    }
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      int check = v[i];
      if (dp[i][y] - dp[i][x - 1] == check) ans++;
    }
    cout << ans << endl;
  }
}
