description: Mr. Chanek gives you a sequence a indexed from 1 to n. Define f(a) as the number of indices where a_i = i. 
  
  You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the 3-rd element from the sequence [4, 2, 3, 1], the resulting sequence will be [4, 2, 1]. 
  
  You want to remove some elements from a in order to maximize f(a), using zero or more operations. Find the largest possible f(a).
  
  Input
  
  The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the initial length of the sequence.
  
  The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 2 ⋅ 10^5) — the initial sequence a.
  
  Output
  
  Output an integer denoting the largest f(a) that can be obtained by doing zero or more operations.
  
  Examples
  
  Input
  
  
  7
  2 1 4 2 5 3 7
  
  
  Output
  
  
  3
  
  
  Input
  
  
  4
  4 2 3 1
  
  
  Output
  
  
  2
  
  Note
  
  In the first example, f(A) = 3 by doing the following operations.
  
  [2,1,4,2,5,3,7] → [2,1,2,5,3,7] → [1,2,5,3,7] → [1,2,5,3] → [1,2,3]
  
  In the second example, f(A) = 2 and no additional operation is needed.
solution: #include <bits/stdc++.h>
  using namespace std;
  template <typename T>
  struct BottomUpSegmentTree {
    int n;
    int N;
    vector<T> tree;
    T (*merge)(T, T);
    T defaultValue;
    BottomUpSegmentTree(int n, T defaultValue, T (*merge)(T, T)) {
      this->n = n;
      this->N = 2 * n;
      this->merge = merge;
      this->defaultValue = defaultValue;
      tree = vector<T>(N, defaultValue);
    }
    BottomUpSegmentTree(vector<T>& arr, T defaultValue, T (*merge)(T, T))
        : BottomUpSegmentTree(arr.size(), defaultValue, merge) {
      build(arr);
    }
    void build(vector<T>& arr) {
      for (int i = 0; i < n; i++) tree[n + i] = arr[i];
      for (int i = n - 1; i > 0; --i)
        tree[i] = merge(tree[i << 1], tree[i << 1 | 1]);
    }
    void updateTreeNode(int p, T value) {
      tree[p + n] = value;
      p = p + n;
      for (int i = p; i > 1; i >>= 1) tree[i >> 1] = merge(tree[i], tree[i ^ 1]);
    }
    T query(int l, int r) {
      T res = defaultValue;
      r++;
      for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
        if (l & 1) res = merge(res, tree[l++]);
        if (r & 1) res = merge(res, tree[--r]);
      }
      return res;
    }
  };
  int merge(int x1, int x2) { return max(x1, x2); }
  int main() {
    int n;
    scanf("%d", &n);
    vector<int> nums(n);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &nums[i]);
      if (nums[i] - i - 1 <= 0) cnt++;
    }
    vector<pair<int, int>> arr(cnt);
    cnt = 0;
    for (int i = 0; i < n; i++) {
      if (nums[i] - i - 1 <= 0) {
        arr[cnt] = {nums[i], -i};
        cnt++;
      }
    }
    vector<int> dp(cnt, 1);
    int ans = 0;
    sort(arr.begin(), arr.end());
    BottomUpSegmentTree<int> tree(n, 0, merge);
    for (int i = 0; i < cnt; i++) {
      int index = -arr[i].second - arr[i].first + 1;
      int val;
      if (index >= 0)
        val = tree.query(0, index);
      else
        val = 0;
      if (dp[i] <= val) dp[i] = val + 1;
      ans = max(ans, dp[i]);
      tree.updateTreeNode(index, dp[i]);
    }
    printf("%d", ans);
    return 0;
  }
  