#include <bits/stdc++.h>
using namespace std;
double tick() {
  static clock_t oldt, newt = clock();
  double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC;
  oldt = newt;
  return diff;
}
long long int a[100005];
int cnt[100005];
long long int ans[100005];
int answer = 0;
struct node {
  int L, R, i;
} q[100005];
bool cmp(node x, node y) {
  if (x.L / 448 != y.L / 448) {
    return x.L / 448 < y.L / 448;
  }
  return x.R < y.R;
}
void add(int position) {
  if (a[position] > 100000) {
    return;
  }
  if (a[position] == cnt[a[position]]) {
    answer--;
  }
  if (a[position] - 1 == cnt[a[position]]) {
    answer++;
  }
  cnt[a[position]]++;
}
void remove(int position) {
  if (a[position] > 100000) {
    return;
  }
  if (a[position] == cnt[a[position]]) {
    answer--;
  }
  if (a[position] + 1 == cnt[a[position]]) {
    answer++;
  }
  cnt[a[position]]--;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    add(i);
  }
  for (int i = 0; i < m; i++) {
    cin >> q[i].L >> q[i].R;
    q[i].L--;
    q[i].R--;
    q[i].i = i;
  }
  sort(q, q + m, cmp);
  int currentL = 0, currentR = n - 1;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR < R) {
      add(currentR + 1);
      currentR++;
    }
    while (currentR > R) {
      remove(currentR);
      currentR--;
    }
    ans[q[i].i] = answer;
  }
  for (int i = 0; i < m; i++) cout << ans[i] << '\n';
}
