<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>

#define ff first
#define ss second
#define MP make_pair
class FW {  
    public: 
    int n;  
    vector<vector<pair<int, int>>> root;  
    FW(int n) { 
        this->n = n;    
        root.resize(n + 1, vector<pair<int, int>>(2, {-1, -1}));
    }

    void update(int id, int x, int val) {  
        // keeps track of two largest value with different x-coordinates
        while(id <= n) {  
            if(val > root[id][0].ff) {
                if(root[id][0].ss != root[id][1].ss) swap(root[id][0], root[id][1]);
                root[id][0] = MP(val, x);
            }   
            else if(x != root[id][0].ss) {
                root[id][1] = max(root[id][1], MP(val, x));
            }
            id += (id & -id);
        }
    }
    
    int get(int id, int x) {   
        int res = 0;    
        while(id > 0) { 
            if(root[id][0].ss == x) res = max(res, root[id][1].ff);
            else res = max(res, root[id][0].ff);
            id -= (id & -id);
        }
        return res;
    }
    
};

class Solution {
public:
    int maxEnvelopes(vector<vector<int>>& c) {
        int n = c.size();
        vector<int> tmp;
        for(int i = 0; i < n; i++) {
            c[i].push_back(i);
            tmp.push_back(c[i][1]);
        }
        // coordinates compression
        sort(begin(c), end(c));
        sort(begin(tmp), end(tmp));
        tmp.erase(unique(begin(tmp), end(tmp)), end(tmp));
        unordered_map<int, int> mp;
        int m = tmp.size();
        for(int i = 0; i < m; i++) {
            mp[tmp[i]] = i + 1;
        }
        int res = 0;
        FW root(m);
        for(auto& it : c) {
            int x = it[0], y = it[1], index = it[2];
            y = mp[y];
            int val = root.get(y - 1, x) + 1;
            res = max(res, val);
            root.update(y, x, val);
        }
        return res;
    }
};
113371