<p>You are given a 2D array of integers <code>envelopes</code> where <code>envelopes[i] = [w<sub>i</sub>, h<sub>i</sub>]</code> represents the width and the height of an envelope.</p>

<p>One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope&#39;s width and height.</p>

<p>Return <em>the maximum number of envelopes you can Russian doll (i.e., put one inside the other)</em>.</p>

<p><strong>Note:</strong> You cannot rotate an envelope.</p>

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

<pre>
<strong>Input:</strong> envelopes = [[5,4],[6,4],[6,7],[2,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The maximum number of envelopes you can Russian doll is <code>3</code> ([2,3] =&gt; [5,4] =&gt; [6,7]).
</pre>

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

<pre>
<strong>Input:</strong> envelopes = [[1,1],[1,1],[1,1]]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= envelopes.length &lt;= 10<sup>5</sup></code></li>
	<li><code>envelopes[i].length == 2</code></li>
	<li><code>1 &lt;= w<sub>i</sub>, h<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        int n = envelopes.size();
        vector<int> segTree(4 * 1e5, 0), lazy(4 * 1e5, 0);
        vector<vector<int>> buckets(1e5 + 1);
        for (int i = 0; i < n ; i++) {
            buckets[envelopes[i][0]].push_back(envelopes[i][1]);
        }
        int ans = 1;
        for (int i = 0; i < buckets.size(); i++) {
            sort(buckets[i].begin(), buckets[i].end());
            vector<int> v;
            for (int j = 0; j < buckets[i].size(); j++) {
                int d = query(segTree, lazy, 1, 0, 1e5, 0, buckets[i][j] - 1);
                ans = max(ans, d + 1);
                v.push_back(d + 1);
            }
            for (int j = 0; j < buckets[i].size(); j++) {
                update(segTree, lazy, 1, 0, 1e5, buckets[i][j], 1e5, v[j]);
            }
        }
        return ans;
    }

private:
    int query(vector<int>& segTree, vector<int>& lazy, int node, int start, int end, int l, int r) {
        if (start > end || end < l || start > r) {
            return 0;
        }
        if (lazy[node] != 0) {
            segTree[node] = max(segTree[node], lazy[node]);
            if (start != end) {
                lazy[2 * node] = max(lazy[2 * node], lazy[node]);
                lazy[(2 * node) + 1] = max(lazy[(2 * node) + 1], lazy[node]);
            }
            lazy[node] = 0;
        }
        if (l <= start && end <= r) {
            return segTree[node];
        }
        int mid = (start + end) / 2;
        return max(query(segTree, lazy, (2 * node), start, mid, l, r), 
                    query(segTree, lazy, (2 * node) + 1, mid + 1, end, l, r));
    }

    void update(vector<int>& segTree, vector<int>& lazy, int node, int start, int end, int l, int r, int d) {
        if (lazy[node] != 0) {
            segTree[node] = max(segTree[node], lazy[node]);
            if (start != end) {
                lazy[2 * node] = max(lazy[2 * node], lazy[node]);
                lazy[(2 * node) + 1] = max(lazy[(2 * node) + 1], lazy[node]);
            }
            lazy[node] = 0;
        }
        if (end < start || end < l || start > r) {
            return;
        }
        if (l <= start && end <= r) {
            segTree[node] = max(segTree[node], d);
            if (start != end) {
                lazy[(2 * node)] = max(lazy[(2 * node)], d);
                lazy[(2 * node) + 1] = max(lazy[(2 * node) + 1], d);
            }
            return;
        }
        int mid = (start + end) / 2;
        update(segTree, lazy, (2 * node), start, mid, l, r, d);
        update(segTree, lazy, (2 * node) + 1, mid + 1, end, l, r, d);
        segTree[node] = max(segTree[(2 * node)], segTree[(2 * node) + 1]);
    }
};

/*
2, 3
5, 4
6, 4
6, 7
*/
287409