#include <bits/stdc++.h>
int N, Q, Am[450][100005], A[100005];
using namespace std;
vector<int> O[100005], U;
int main() {
  cin >> N >> Q;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (A[i] <= N) O[A[i]].push_back(i);
  }
  for (int i = 1; i <= N; i++)
    if (O[i].size() >= i) U.push_back(i);
  for (int i = 0; i < U.size(); i++)
    for (int p = 1; p <= N; p++) Am[i][p] = Am[i][p - 1] + (A[p] == U[i]);
  while (Q--) {
    int l, r;
    cin >> l >> r;
    int ans = 0;
    for (int i = 0; i < U.size(); i++)
      if (Am[i][r] - Am[i][l - 1] == U[i]) ans++;
    cout << ans << "\n";
  }
}
