#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
const long long big = 1e9 + 1;
const long long block = 800;
const long long mod = 1e6;
long long n, q;
long long a[N];
long long ans[N];
vector<pair<pair<long long, long long>, long long> > x;
unordered_map<long long, long long> y;
long long cur = 0;
bool comp(pair<pair<long long, long long>, long long> b,
          pair<pair<long long, long long>, long long> c) {
  if (b.first.first / block == c.first.first / block)
    return (b.first.second < c.first.second);
  return (b.first.first < c.first.first);
}
void add(long long i) {
  if (y[a[i]] == a[i]) cur--;
  y[a[i]]++;
  if (y[a[i]] == a[i]) cur++;
}
void del(long long i) {
  if (y[a[i]] == a[i]) cur--;
  y[a[i]]--;
  if (y[a[i]] == a[i]) cur++;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = (0); i < (q); i += 1) {
    long long l, r;
    cin >> l >> r;
    x.push_back({{l, r}, i});
  }
  sort((x).begin(), (x).end(), comp);
  long long l = 1, r = 0;
  for (auto u : x) {
    while (r < u.first.second) r++, add(r);
    while (r > u.first.second) del(r), r--;
    while (l < u.first.first) del(l), l++;
    while (l > u.first.first) l--, add(l);
    ans[u.second] = cur;
  }
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
