#include <bits/stdc++.h>
using namespace std;
int dp[100011][511];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g(n + 1);
  map<int, int> dict;
  for (int i = 0; i < n; i++) {
    cin >> g[i];
    dict[g[i]]++;
  }
  vector<int> p;
  map<int, int>::iterator it = dict.begin();
  for (; it != dict.end(); it++)
    if (it->first <= it->second) p.push_back(it->first);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < p.size(); j++) {
      if (!i)
        dp[i][j] = 0;
      else {
        dp[i][j] = dp[i - 1][j];
        if (g[i - 1] == p[j]) dp[i][j]++;
      }
    }
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    int ans = 0;
    for (int j = 0; j < p.size(); j++)
      if (dp[r][j] - dp[l - 1][j] == p[j]) ans++;
    cout << ans << endl;
  }
  return 0;
}
