<p>You are given an integer array <code>arr</code>.</p>

<p>We split <code>arr</code> into some number of <strong>chunks</strong> (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.</p>

<p>Return <em>the largest number of chunks we can make to sort the array</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [5,4,3,2,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn&#39;t sorted.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [2,1,3,4,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong>
We can split into two chunks, such as [2, 1], [3, 4, 4].
However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 2000</code></li>
	<li><code>0 &lt;= arr[i] &lt;= 10<sup>8</sup></code></li>
</ul>

class SegmentTree {
private:
    std::vector<int> tree;
    std::vector<int> data;
    int n;
    int flag;
    int merge(int a, int b){
        if(!flag)return max(a, b);
        return min(a, b);
    }
    void build(int node, int start, int end) {
        if (start == end) {
            tree[node] = data[start];
        } else {
            int mid = (start + end) / 2;
            int leftChild = 2 * node;
            int rightChild = 2 * node + 1;
            build(leftChild, start, mid);
            build(rightChild, mid + 1, end);
            tree[node] = merge(tree[leftChild], tree[rightChild]);
        }
    }
    void update(int node, int start, int end, int idx, int value) {
        if (start == end) {
            data[idx] = value;
            tree[node] = value;
        } else {
            int mid = (start + end) / 2;
            int leftChild = 2 * node;
            int rightChild = 2 * node + 1;

            if (idx <= mid) {
                update(leftChild, start, mid, idx, value);
            } else {
                update(rightChild, mid + 1, end, idx, value);
            }
            tree[node] = merge(tree[leftChild], tree[rightChild]);
        }
    }

    int query(int node, int start, int end, int L, int R) {
        if (R < start || end < L) {
            if(!flag)return 0;
            return INT_MAX;
        }

        if (L <= start && end <= R) {
            return tree[node];
        }
        int mid = (start + end) / 2;
        int leftChild = 2 * node;
        int rightChild = 2 * node + 1;

        int leftQuery = query(leftChild, start, mid, L, R);
        int rightQuery = query(rightChild, mid + 1, end, L, R);

        return merge(leftQuery, rightQuery);
    }

public:
    SegmentTree(const std::vector<int>& input, int f) {
        data = input;
        n = data.size();
        flag = f;
        tree.resize(4 * n); 
        build(1, 0, n - 1);
    }

    // Function to update a value in the array and segment tree
    void update(int idx, int value) {
        update(1, 0, n - 1, idx, value);
    }

    // Function to query the sum over a range [L, R]
    int query(int L, int R) {
        return query(1, 0, n - 1, L, R);
    }
};
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int n = arr.size();
        SegmentTree* s1 = new SegmentTree(arr, 0);
        SegmentTree* s2 = new SegmentTree(arr, 1);
        vector<int> dp(n + 1, -1);
        function<int(int i)> helper;
        helper = [&](int i){
            if(i == n)return 0;
            if(dp[i] != -1)return dp[i];
            int ans = 1;
            for(int j = i; j < n - 1; j++){
                int left = s1->query(i, j), right = s2->query(j + 1, n - 1);
                // cout<<i<<" "<<j<<" "<<left<<" "<<right<<endl;
                if(left <= right){
                    ans = max(ans, 1 + helper(j + 1));
                }
            }
            return dp[i] = ans;
        };
        return helper(0);
    }
};
18200