#include <bits/stdc++.h>
using namespace std;
typedef struct node {
  int val;
  node *left, *right;
  node(int _val = 0, node *_left = 0, node *_right = 0) {
    val = _val;
    left = _left;
    right = _right;
  }
} * Node;
int i, n, a[100005], m, x, y, rs, l, r, nr, poz[100005], last[100005];
set<int> S[100005];
map<int, int> M;
Node rev[100005];
Node build(int left, int right) {
  if (left == right) return new node;
  int pivot = (left + right) / 2;
  Node now = new node;
  now->left = build(left, pivot);
  now->right = build(pivot + 1, right);
  return now;
}
Node update(int left, int right, Node root) {
  if (left == right) {
    Node now = new node(root->val + 1);
    return now;
  }
  int pivot = (left + right) / 2;
  Node now = new node;
  now->left = root->left;
  now->right = root->right;
  if (a[i] <= pivot)
    now->left = update(left, pivot, root->left);
  else
    now->right = update(pivot + 1, right, root->right);
  return now;
}
int query(int left, int right, Node root) {
  if (!root) return 0;
  if (left == right) return root->val;
  int pivot = (left + right) / 2;
  if (x <= pivot) return query(left, pivot, root->left);
  return query(pivot + 1, right, root->right);
}
int main() {
  ios_base::sync_with_stdio(0);
  rev[0] = build(1, 1e5);
  cin >> n >> m;
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    poz[i] = nr;
    last[i] = last[i - 1];
    if (a[i] > 1e5) continue;
    ++M[a[i]];
    poz[i] = ++nr;
    if (M[a[i]] == a[i]) S[i] = S[last[i - 1]], S[nr].insert(a[i]), last[i] = i;
    rev[nr] = update(1, 1e5, rev[nr - 1]);
  }
  for (i = 1; i <= m; ++i) {
    cin >> l >> r;
    rs = 0;
    for (auto it : S[last[poz[r]]]) {
      x = it;
      y = query(1, 1e5, rev[poz[r]]) - query(1, 1e5, rev[poz[l - 1]]);
      if (y == it) ++rs;
    }
    cout << rs << '\n';
  }
  return 0;
}
