#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int n, q, v[N], mx, f[N], l[N], r[N], cum[N], res[N];
int main(int argc, char **argv) {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; ++i) {
    scanf("%d", v + i);
    if (v[i] < N) {
      ++f[v[i]];
      mx = max(mx, v[i]);
    }
  }
  for (int i = 0; i < q; ++i) scanf("%d%d", l + i, r + i);
  for (int i = 1; i <= mx; ++i)
    if (f[i] >= i) {
      for (int j = 0; j < n; ++j) cum[j + 1] = cum[j] + (v[j] == i);
      for (int j = 0; j < q; ++j) res[j] += cum[r[j]] - cum[l[j] - 1] == i;
    }
  for (int i = 0; i < q; ++i) printf("%d\n", res[i]);
  return 0;
}
