#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1);   const int N = 1e6 + 10, M = 1e7 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f, ha = 137;   int n, q;   int mx[N], a[N];   int s[M], ls[M], rs[M];   int rt[N];   int cnt;   int update(int loc, int pre, int l, int r) {     int now = ++cnt;     s[now] = s[pre] + 1;     ls[now] = ls[pre];     rs[now] = rs[pre];     if (l == r) return now;     int mid = l + r >> 1;     if (loc <= mid)       ls[now] = update(loc, ls[pre], l, mid);     else       rs[now] = update(loc, rs[pre], mid + 1, r);     return now;   }   int query(int a, int b) {     int x = rt[a - 1], y = rt[b];     int ans = 0;     int l = 0, r = n;     while (l < r) {       int mid = l + r >> 1;       if (a <= mid)         ans += s[rs[y]] - s[rs[x]], x = ls[x], y = ls[y], r = mid;       else {         l = mid + 1, x = rs[x], y = rs[y];       }     }     return ans;   }   int tot;   struct dat {     int s[N << 2], lazy[N << 2];     void update(int a, int b, int x, int k, int l, int r) {       if (a == l && b == r) {         s[x] += k;         lazy[x] += k;         return;       }       int mid = l + r >> 1, ls = x << 1, rs = x << 1 | 1;       if (lazy[x]) {         update(l, mid, ls, lazy[x], l, mid);         update(mid + 1, r, rs, lazy[x], mid + 1, r);         lazy[x] = 0;       }       if (b <= mid)         update(a, b, ls, k, l, mid);       else if (a > mid)         update(a, b, rs, k, mid + 1, r);       else {         update(a, mid, ls, k, l, mid);         update(mid + 1, b, rs, k, mid + 1, r);       }       s[x] = max(s[ls], s[rs]);     }     int query(int k) {       int x = 1;       int l = 1, r = n;       while (l < r) {         int mid = l + r >> 1, ls = x << 1, rs = x << 1 | 1;         if (lazy[x]) {           update(l, mid, ls, lazy[x], l, mid);           update(mid + 1, r, rs, lazy[x], mid + 1, r);           lazy[x] = 0;         }         if (s[rs] >= k)           x = rs, l = mid + 1;         else           x = ls, r = mid;       }       return l;     }   } tr;   int f[N];   int ss;   int main() {     cin >> n >> q;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       int k = i - a[i];       if (k < 0) {         rt[i] = update(a[i] = 0, rt[i - 1], 0, n);         continue;       }       int t = tr.query(k);       a[i] = f[t];       if (k == 0) f[++tot] = i, a[i] = i, t = tot;       if (k > ss) {         a[i] = 0, t = 0;       } else         ss++;       if (min(tot, t)) tr.update(1, min(tot, t), 1, 1, 1, n);       rt[i] = update(a[i], rt[i - 1], 0, n);     }     int l, r;     while (q--) {       scanf( %d%d , &l, &r);       printf( %d n , query(l, n - r));     }     return 0;   }   