#include <bits/stdc++.h>
const int N = 100005;
int n, m, a[N], l[N], r[N], dp[N], cnt[N], ans[N];
void input() {
  scanf("%d%d", &n, &m);
  n++;
  m++;
  for (int i = 1; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] < n) cnt[a[i]]++;
  }
  for (int i = 1; i < m; i++) scanf("%d%d\n", &l[i], &r[i]);
}
void solve() {
  for (int i = 1; i < n; i++) {
    if (cnt[i] >= i) {
      dp[0] = 0;
      for (int j = 1; j < n; j++) dp[j] = a[j] != i ? dp[j - 1] : dp[j - 1] + 1;
      for (int j = 1; j < m; j++)
        if (dp[r[j]] - dp[l[j] - 1] == i) ans[j]++;
    }
  }
}
void output() {
  for (int i = 1; i < m; i++) printf("%d\n", ans[i]);
}
int main() {
  input();
  solve();
  output();
  return 0;
}
