#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int niz[100001];
int bit[100001][640];
int ids[640];
set<int> bst;
map<int, int> mst;
map<int, int> idd;
int id = 0;
inline void update(int x, int val, int idx) {
  while (x <= n) {
    bit[x][idx] += val;
    x += (x & -x);
  }
}
inline int read(int x, int idx) {
  int ret = 0;
  while (x > 0) {
    ret += bit[x][idx];
    x -= (x & -x);
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &niz[i]);
    if (mst[niz[i]] == niz[i] - 1) {
      bst.insert(niz[i]);
      idd[niz[i]] = id;
      ids[id++] = niz[i];
      mst[niz[i]]++;
    } else
      mst[niz[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (bst.find(niz[i]) != bst.end()) {
      update(i, 1, idd[niz[i]]);
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    int sol = 0;
    for (int j = 0; j < id; j++) {
      if (read(b, j) - read(a - 1, j) == ids[j]) sol++;
    }
    printf("%d\n", sol);
  }
  return 0;
}
