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

    static bool compare(vector<int>&a,vector<int>&b){
        if(a[0]==b[0]){
        if(a[1]<b[1]) return false;
        return true;
        }
        if(a[0]<b[0]) return true;
        return  false;
    }

    class segmentTree{
        public:
        vector<int>seg;int len;
        segmentTree(int n){
            seg.assign(4*n,0);
            len=4*n;
        }

        void update(int index,int l,int r ,int i,int value){
            //cout<<l<<" "<<r<<endl;
            if(r==l && r==i ){
                //cout<<-1<<" "<<index<<endl;
                seg[index]=value; return;
            }

            if(l>r) return ;
            int mid=(l+r)>>1;

           if(i<=mid) update(2*index+1,l,mid,i,value);
            else update(2*index+2,mid+1,r,i,value);
            seg[index]=max(seg[2*index+1],seg[2*index+2]);
        }
        
        int query(int index,int low,int high,int l,int r){
            if(low>=l && high<=r) return seg[index];
                if(low>r || high<l) return 0;
            int mid=(low+high)>>1;
            int left=query(2*index+1,low,mid,l,r);
            int right=query(2*index+2,mid+1,high,l,r);
            return max(left,right);
        }

    };
    int maxEnvelopes(vector<vector<int>>& a) {
        
    sort(a.begin(),a.end(),compare);
    int n=a.size();
    for(auto i:a) cout<<i[1]<<" "; cout<<endl;
    segmentTree Tr(1e5+1);
    int ans=0;
    for(int i=0;i<n;i++){
        int value=Tr.query(0,0,1e5,0,a[i][1]-1);
        ans=max(ans,value+1);
        Tr.update(0,0,1e5,a[i][1],value+1);
    }
    return ans;
    }
};
195271