#include <bits/stdc++.h>
using namespace std;
bool myfunction(int i, int j) { return (i < j); }
int n, m;
int a[100001];
map<int, int> mp;
vector<int> st;
vector<int> cc[100001];
map<pair<int, int>, int> mpp;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (a[i] > n) continue;
    mp[a[i]]++;
  }
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
    int num = (*it).first, cnt = (*it).second;
    if (num <= cnt) st.push_back(num);
  }
  mp.clear();
  for (int i = 1; i <= n; ++i) {
    mp[a[i]]++;
    for (int j = 0; j < st.size(); ++j) {
      cc[i].push_back(mp[st[j]]);
    }
  }
  mp.clear();
  for (int i = 1; i <= m; ++i) {
    int l, r, ret = 0;
    scanf("%d%d", &l, &r);
    for (int j = 0; j < st.size(); ++j) {
      int hi = cc[r][j], lo;
      if (l == 1)
        lo = 0;
      else
        lo = cc[l - 1][j];
      if (hi - lo == st[j]) ++ret;
    }
    printf("%d\n", ret);
  }
  return 0;
}
