<p>Given an array <code>intervals</code> where <code>intervals[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represent the interval <code>[l<sub>i</sub>, r<sub>i</sub>)</code>, remove all intervals that are covered by another interval in the list.</p>

<p>The interval <code>[a, b)</code> is covered by the interval <code>[c, d)</code> if and only if <code>c &lt;= a</code> and <code>b &lt;= d</code>.</p>

<p>Return <em>the number of remaining intervals</em>.</p>

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

<pre>
<strong>Input:</strong> intervals = [[1,4],[3,6],[2,8]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Interval [3,6] is covered by [2,8], therefore it is removed.
</pre>

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

<pre>
<strong>Input:</strong> intervals = [[1,4],[2,3]]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= intervals.length &lt;= 1000</code></li>
	<li><code>intervals[i].length == 2</code></li>
	<li><code>0 &lt;= l<sub>i</sub> &lt; r<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li>All the given intervals are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    #define deb2(x,y) cout<<#x<<" is "<<x<<" and "<<#y<<" is "<<y<<endl;
    #define deb(x) cout<<#x<<" is "<<x<<endl;
    int removeCoveredIntervals(vector<vector<int>>& intervals) {
       multiset<int> front;
       vector<pair<int,int>> pts;
       int ans = 0;
       int n = intervals.size();
       for(int i = 0;i<intervals.size();i++)
       {
        int l = intervals[i][0],r=intervals[i][1];
        pts.push_back({l,-(r-l)});  
        pts.push_back({r,r-l});  
       }
       sort(pts.begin(),pts.end());
       for(auto x:pts)
       {
        // deb2(x.first,x.second);
        if(x.second<0)
        {
            front.insert(x.first);
        }
        else 
        {
            int l = x.first - x.second;
            // deb(l);
            front.erase(front.find(l));
            // deb(front.size()); 
            auto itr = front.lower_bound(l);
            // for(auto it = front.begin();it!=front.end();it++)
            //     cout<<*it<<" ";
            // cout<<endl;
            int dist = distance(front.begin(),itr);
            // deb(*itr);
            // deb(dist);
            // if(itr==front.end())
            //     cout<<"True"<<endl;
            if(front.empty())
                continue;
            if(itr==front.begin()&&*front.begin()==l)
               ans++;
            else if(itr!=front.begin())
                ans++;
            // deb(ans);
        }
        // deb(front.size());
        //     cout<<endl;
       } 
       return n - ans;
    }
};
17000