#include <bits/stdc++.h>
using namespace std;
int B = sqrt(1e5);
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / B != b.first.first / B)
    return a.first.first < b.first.first;
  return (a.first.first / B & 1) ? (a.first.second < b.first.second)
                                 : (a.first.second > b.first.second);
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), fre(n + 1, 0), ans(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] > n) a[i] = 0;
  }
  vector<pair<pair<int, int>, int> > q(m);
  for (int i = 0; i < m; ++i) {
    cin >> q[i].first.first >> q[i].first.second;
    q[i].second = i;
  }
  sort(q.begin(), q.end(), cmp);
  int cur = 0, l = q[0].first.first - 1, r = l - 1;
  for (int i = 0; i < m; ++i) {
    int ll = q[i].first.first, rr = q[i].first.second, idx = q[i].second;
    --ll, --rr;
    while (l < ll) {
      if (fre[a[l]] == a[l] && a[l]) cur--;
      fre[a[l]]--;
      if (fre[a[l]] == a[l] && a[l]) cur++;
      l++;
    }
    while (r < rr) {
      ++r;
      if (fre[a[r]] == a[r] && a[r]) cur--;
      fre[a[r]]++;
      if (fre[a[r]] == a[r] && a[r]) cur++;
    }
    while (l > ll) {
      --l;
      if (fre[a[l]] == a[l] && a[l]) cur--;
      fre[a[l]]++;
      if (fre[a[l]] == a[l] && a[l]) cur++;
    }
    while (r > rr) {
      if (fre[a[r]] == a[r] && a[r]) cur--;
      fre[a[r]]--;
      if (fre[a[r]] == a[r] && a[r]) cur++;
      r--;
    }
    ans[idx] = cur;
  }
  for (auto i : ans) cout << i << '\n';
}
