#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int Max = 1e5 + 1;
const long long mod1 = 998244353;
int block;
struct Query {
  int L, R, index;
};
bool compare(Query x, Query y) {
  if (x.L / block != y.L / block) return y.L / block < x.L / block;
  return x.R < y.R;
}
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
void convert(int arr[], int n) {
  int temp[n];
  memcpy(temp, arr, n * sizeof(int));
  sort(temp, temp + n);
  unordered_set<int> mp;
  unordered_map<int, int> umap;
  int val = 0;
  for (int i = 0; i < n; i++) {
    if (mp.find(temp[i]) == mp.end()) {
      umap[temp[i]] = val;
      val++;
      mp.insert(temp[i]);
    }
  }
  for (int i = 0; i < n; i++) arr[i] = umap[arr[i]];
}
void mo(Query query[], int arr[], int n, int q) {
  int freq[n + 1];
  for (int i = 0; i <= n; i++) {
    freq[i] = 0;
  }
  int ml = query[0].L, mr = query[0].L - 1;
  int element, f;
  int ans[q];
  int result = 0;
  for (int i = 0; i < q; i++) {
    int l = query[i].L, r = query[i].R;
    while (ml < l) {
      element = arr[ml];
      if (element > n) {
        continue;
      }
      freq[element]--;
      f = freq[element];
      if (element == f) {
        result++;
      }
      if (element - 1 == f) {
        result--;
      }
      ml++;
    }
    while (ml > l) {
      ml--;
      element = arr[ml];
      if (element > n) {
        continue;
      }
      freq[element]++;
      f = freq[element];
      if (element == f) {
        result++;
      }
      if (element + 1 == f) {
        result--;
      }
    }
    while (mr < r) {
      mr++;
      element = arr[mr];
      if (element > n) {
        continue;
      }
      freq[element]++;
      f = freq[element];
      if (element == f) {
        result++;
      }
      if (element + 1 == f) {
        result--;
      }
    }
    while (mr > r) {
      element = arr[mr];
      if (element > n) {
        continue;
      }
      freq[element]--;
      f = freq[element];
      if (element == f) {
        result++;
      }
      if (element - 1 == f) {
        result--;
      }
      mr--;
    }
    ans[query[i].index] = result;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    int n, q;
    cin >> n >> q;
    int arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    Query query[q];
    int l, r;
    for (int i = 0; i < q; i++) {
      cin >> l >> r;
      query[i] = {l - 1, r - 1, i};
    }
    block = sqrt(n);
    sort(query, query + q, compare);
    mo(query, arr, n, q);
  }
}
