1class Solution {2public:3vector<int> tree;4void build(vector<int> &nums) {5int n = nums.size();6for (int i = 0; i < nums.size(); i++) tree[i + n] = nums[i];7for (int i = n - 1; i > 0; i--) tree[i] = min(tree[i << 1], tree[i << 1 | 1]);8}910int query(int l, int r, int n) {11l += n, r += n;12int ans = INT_MAX;13while (l < r) {14if (l & 1) ans = min(ans, tree[l++]);15if (r & 1) ans = min(ans, tree[--r]);16l >>= 1;17r >>= 1;18}19return ans;20}2122int partitionDisjoint(vector<int> &nums) {23int n = nums.size();24int mx = -1;25tree.resize(2 * n, INT_MAX);26build(nums);27for (int left = 0; left < n; left++) {28mx = max(mx, nums[left]);29if (query(left + 1, n, n) >= mx) return left + 1;30}31return n;32}33};