#include <bits/stdc++.h>
using namespace std;
struct qry {
  int L;
  int R;
  int id;
};
bool cmp(qry q1, qry q2) {
  if (q1.R == q2.R) return q1.L < q2.L;
  return q1.R < q2.R;
}
int n, m;
int ara[100000 + 5];
int blcSize, blcCnt;
vector<qry> blocks[350 + 2];
int ans[100000];
int main() {
  cin >> n >> m;
  blcSize = sqrt(n);
  blcCnt = n / blcSize;
  if (n % blcSize) blcCnt++;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ara[i] = x;
  }
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    blocks[l / blcSize].push_back({l, r, i});
  }
  for (int blc = 0; blc < blcCnt; blc++) {
    if (blocks[blc].size() == 0) continue;
    sort(blocks[blc].begin(), blocks[blc].end(), cmp);
    unordered_map<int, int> frq;
    int okay = 0;
    int l = blocks[blc][0].R + 1;
    int r = l - 1;
    for (int j = 0; j < blocks[blc].size(); j++) {
      qry q = blocks[blc][j];
      while (l < q.L) {
        int& val = frq[ara[l]];
        if (val == ara[l]) okay--;
        val--;
        if (val == ara[l]) okay++;
        l++;
      }
      while (l > q.L) {
        l--;
        int& val = frq[ara[l]];
        if (val == ara[l]) okay--;
        val++;
        if (val == ara[l]) okay++;
      }
      while (r < q.R) {
        r++;
        int& val = frq[ara[r]];
        if (val == ara[r]) okay--;
        val++;
        if (val == ara[r]) okay++;
      }
      while (r > q.R) {
        int& val = frq[ara[r]];
        if (val == ara[r]) okay--;
        val--;
        if (val == ara[r]) okay++;
        r--;
      }
      ans[q.id] = okay;
    }
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}
