<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:
    void update(int idx, vector<int> &tree, int li, int ri, int qIdx, int val) {
        if (ri < li || ri < qIdx || li > qIdx) return;
        if (li == ri) {
            tree[idx] = val;
            return;
        }
        int mid = (li+ri)/2;
        update((2*idx), tree, li, mid, qIdx, val);
        update((2*idx)+1, tree, mid+1, ri, qIdx, val);
        tree[idx] = max(tree[(2*idx)], tree[(2*idx)+1]);
        return;
    }
    int query(int idx, vector<int> &tree, int li, int ri, int qli, int qri) {
        if (ri < li || qri < qli || ri < qli || li > qri) return 0;
        if (li >= qli && ri <= qri) return tree[idx];
        int mid = (li+ri)/2;
        return max(query((2*idx), tree, li, mid, qli, qri), query((2*idx)+1, tree, mid+1, ri, qli, qri));
    }
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        vector<pair<int,int>> v;
        int n = envelopes.size();
        for(int i = 0; i<n; i++) v.push_back({envelopes[i][0], envelopes[i][1]});
        sort(v.begin(), v.end(), greater<pair<int,int>>());
        vector<int> tree(400005, 0);
        vector<int> hash(100005, 0);
        int currW = -1;
        queue<pair<int,int>> pending;
        for(int i = 0; i<n; i++) {
            if (v[i].first < currW) {
                // clear pending updates
                while(!pending.empty()) {
                    int h = (pending.front()).first;
                    int val = (pending.front()).second;
                    pending.pop();
                    if (hash[h] < val) {
                        hash[h] = val;
                        update(1, tree, 0, 100000, h, val);
                    }
                }
            }
            currW = v[i].first;
            int mx = query(1, tree, 0, 100000, v[i].second+1, 100000);
            pending.push({v[i].second, 1+mx});
        }
        while(!pending.empty()) {
            int h = (pending.front()).first;
            int val = (pending.front()).second;
            pending.pop();
            if (hash[h] < val) {
                hash[h] = val;
                update(1, tree, 0, 100000, h, val);
            }
        }
        return tree[1];
    }
};
261815