#include <bits/stdc++.h>
using namespace std;
int n, q, num[100010];
pair<int, pair<int, int> > qry[100010];
int ans[100010];
vector<int> lst[100010];
int tree[100010];
void add(int pos, int v) {
  for (; pos <= n; pos += pos & (-pos)) tree[pos] += v;
}
int query(int pos) {
  int ret = 0;
  for (; pos; pos -= pos & (-pos)) ret += tree[pos];
  return ret;
}
int main() {
  int i, j, siz;
  scanf("%d%d", &n, &q);
  for (i = 1; i <= n; i++) scanf("%d", &num[i]);
  for (i = 1; i <= q; i++) {
    scanf("%d%d", &qry[i].second.first, &qry[i].first);
    qry[i].second.second = i;
  }
  sort(qry + 1, qry + 1 + q);
  for (i = j = 1; i <= n; i++)
    if (num[i] <= n) {
      lst[num[i]].push_back(i);
      if ((siz = lst[num[i]].size()) >= num[i]) {
        if (siz > num[i] + 1) add(lst[num[i]][siz - num[i] - 2], 1);
        if (siz > num[i]) add(lst[num[i]][siz - num[i] - 1], -2);
        add(lst[num[i]][siz - num[i]], 1);
      }
      for (; qry[j].first < i && j <= q; j++)
        ;
      for (; qry[j].first == i; j++)
        ans[qry[j].second.second] =
            query(qry[j].first) - query(qry[j].second.first - 1);
    }
  for (i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
