#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, num;
} a[100005];
int n, t, m, k, i, j, s[100005], b[450], h[100005][450];
map<int, int> mymap;
int main() {
  while (~scanf("%d%d", &n, &t)) {
    mymap.clear();
    m = 0;
    for (i = 1; i <= n; i++) scanf("%d", &s[i]);
    for (i = 1; i <= n; i++) {
      if (mymap.find(s[i]) == mymap.end()) {
        mymap[s[i]] = ++m;
        a[m].x = s[i];
        a[m].num = 0;
      }
      a[mymap[s[i]]].num++;
    }
    k = 0;
    for (i = 1; i <= m; i++)
      if (a[i].x <= a[i].num) b[++k] = a[i].x;
    for (i = 1; i <= k; i++) {
      h[0][i] = 0;
      for (j = 1; j <= n; j++)
        if (s[j] == b[i])
          h[j][i] = h[j - 1][i] + 1;
        else
          h[j][i] = h[j - 1][i];
    }
    int l, r, ans;
    while (t--) {
      ans = 0;
      scanf("%d%d", &l, &r);
      for (i = 1; i <= k; i++)
        if (h[r][i] - h[l - 1][i] == b[i]) ans++;
      printf("%d\n", ans);
    }
  }
  return 0;
}
