#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<int> a(n + 1), l(m), r(m), freq(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] <= n) {
      freq[a[i]]++;
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &l[i], &r[i]);
  }
  vector<int> ans(m, 0), cand;
  for (int i = 1; i <= n; i++) {
    if (freq[i] >= i) cand.push_back(i);
  }
  for (int x : cand) {
    vector<int> cnt(n + 1, 0);
    for (int i = 1; i <= n; i++)
      cnt[i] += (a[i] == x ? 1 + cnt[i - 1] : cnt[i - 1]);
    int res = 0;
    for (int i = 0; i < m; i++) ans[i] += (cnt[r[i]] - cnt[(l[i] - 1)] == x);
  }
  for (int val : ans) printf("%d\n", val);
  return 0;
}
