#include <bits/stdc++.h>
using namespace std;
int n, m, dp[500][100005], num[100005], vis[100005], st[100005], a[500];
int main() {
  memset(dp, 0, sizeof(0));
  memset(vis, 0, sizeof(vis));
  memset(st, 0, sizeof(st));
  scanf("%d%d", &n, &m);
  int top = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    if (num[i] <= n) {
      vis[num[i]]++;
    }
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] >= i) a[top++] = i;
  for (int i = 0; i < top; i++)
    for (int j = 1; j <= n; j++)
      dp[i][j] = a[i] == num[j] ? dp[i][j - 1] + 1 : dp[i][j - 1];
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int cnt = 0;
    for (int i = 0; i < top; i++)
      if (dp[i][r] - dp[i][l - 1] == a[i]) cnt++;
    printf("%d\n", cnt);
  }
  return 0;
}
