#include <bits/stdc++.h>
using namespace std;
struct Query {
  long long i, a, b;
};
long long M[100005];
long long w;
Query Q[100005];
long long R[100005];
long long A[100005];
bool comp(Query q1, Query q2) {
  long long b1 = q1.a / w;
  long long b2 = q2.a / w;
  if (b1 != b2) {
    return b1 < b2;
  } else {
    return q1.b < q2.b;
  }
}
int main() {
  long long n, m;
  cin >> n >> m;
  w = sqrt(n);
  for (long long i = 0; i < n; i++) {
    cin >> M[i];
  }
  for (long long i = 0; i < m; i++) {
    Q[i].i = i;
    cin >> Q[i].a >> Q[i].b;
    Q[i].a--;
    Q[i].b--;
  }
  sort(Q, Q + m, comp);
  long long a = 0, b = -1;
  long long r = 0;
  for (long long i = 0; i < m; i++) {
    while (a < Q[i].a) {
      if (M[a] <= n) {
        if (A[M[a]] == M[a]) r--;
        A[M[a]]--;
        if (A[M[a]] == M[a]) r++;
      }
      a++;
    }
    while (a > Q[i].a) {
      a--;
      if (M[a] <= n) {
        if (A[M[a]] == M[a]) r--;
        A[M[a]]++;
        if (A[M[a]] == M[a]) r++;
      }
    }
    while (b < Q[i].b) {
      b++;
      if (M[b] <= n) {
        if (A[M[b]] == M[b]) r--;
        A[M[b]]++;
        if (A[M[b]] == M[b]) r++;
      }
    }
    while (b > Q[i].b) {
      if (M[b] <= n) {
        if (A[M[b]] == M[b]) r--;
        A[M[b]]--;
        if (A[M[b]] == M[b]) r++;
      }
      b--;
    }
    R[Q[i].i] = r;
  }
  for (long long i = 0; i < m; i++) {
    cout << R[i] << "\n";
  }
}
