func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   int a[N], ret[N];   pair<pair<int, int>, int> qi[N];   int pen[N];   void update(int ind, int add) {     while (ind < N) {       pen[ind] += add;       ind += ind & (-ind);     }   }   int query(int ind) {     int ret = 0;     while (ind > 0) {       ret = ret + pen[ind];       ind = ind & (ind - 1);     }     return ret;   }   pair<int, int> seg[N * 4];   int lazy[N * 4];   void init(int pos, int l, int r) {     if (l == r) {       seg[pos] = {a[l], l};       return;     }     int mid = (l + r) / 2;     init(pos * 2, l, mid);     init(pos * 2 + 1, mid + 1, r);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void push(int pos, int l, int r) {     if (lazy[pos] != 0) {       seg[pos].first += lazy[pos];       if (l != r) {         lazy[pos * 2] += lazy[pos];         lazy[pos * 2 + 1] += lazy[pos];       }       lazy[pos] = 0;     }   }   pair<int, int> query(int pos, int l, int r, int ql, int qr) {     push(pos, l, r);     if (r < ql || qr < l) {       return {-1e9, 0};     }     if (ql <= l && r <= qr) {       return seg[pos];     }     int mid = (l + r) / 2;     return max(query(pos * 2, l, mid, ql, qr),                query(pos * 2 + 1, mid + 1, r, ql, qr));   }   void update(int pos, int l, int r, int ql, int qr, int val) {     push(pos, l, r);     if (r < ql || qr < l) {       return;     }     if (ql <= l && r <= qr) {       lazy[pos] = val;       push(pos, l, r);       return;     }     int mid = (l + r) / 2;     update(pos * 2, l, mid, ql, qr, val);     update(pos * 2 + 1, mid + 1, r, ql, qr, val);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void update(int pos, int l, int r, int ind, pair<int, int> val) {     push(pos, l, r);     if (r < ind || ind < l) {       return;     }     if (l == r) {       seg[pos] = val;       return;     }     int mid = (l + r) / 2;     update(pos * 2, l, mid, ind, val);     update(pos * 2 + 1, mid + 1, r, ind, val);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void solve() {     int n, q;     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);     }     int t = 0;     for (int i = 1; i <= n; ++i) {       if (i - t <= a[i] && a[i] <= i) {         a[i] = i - a[i] - t;         ++t;         update(i, 1);       } else {         a[i] = -1e9;       }     }     init(1, 1, n);     for (int i = 0; i < q; ++i) {       scanf( %d %d  , &qi[i].first.first, &qi[i].first.second);       qi[i].second = i;     }     sort(qi, qi + q);     int pos = 0;     for (int i = 0; i < q; ++i) {       int x = qi[i].first.first, y = qi[i].first.second;       while (pos + 1 <= x) {         ++pos;         if (query(1, 1, n, pos, pos).first == 0) {           update(1, 1, n, pos + 1, n, 1);           update(1, 1, n, pos, {-1e9, 0});           update(pos, -1);         }         while (true) {           pair<int, int> qr = query(1, 1, n, 1, n);           if (qr.first <= 0) {             break;           }           update(qr.second, -1);           update(1, 1, n, qr.second, {-1e9, 0});           update(1, 1, n, qr.second, n, 1);         }       }       ret[qi[i].second] = query(n - y);     }     for (int i = 0; i < q; ++i) {       printf( %d n , ret[i]);     }   }   int main() {     solve();     return 0;   }   