#include <bits/stdc++.h>
using namespace std;
int n, m, res[100002];
pair<int, int> query[100002];
vector<int> ele[100002];
int main(int argc, char **argv) {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    if (x <= n) ele[x].push_back(i);
  }
  for (int i = 1; i <= m; ++i) scanf("%d%d", &query[i].first, &query[i].second);
  for (int i = 1; i <= n; ++i)
    if (ele[i].size() >= i)
      for (int j = 1; j <= m; ++j) {
        int u = query[j].first, v = query[j].second;
        int l = lower_bound(ele[i].begin(), ele[i].end(), u) - ele[i].begin();
        int r = upper_bound(ele[i].begin(), ele[i].end(), v) - ele[i].begin();
        res[j] += (r - l == i);
      }
  for (int i = 1; i <= m; ++i) printf("%d\n", res[i]);
  return 0;
}
