<p>Given an array <code>rectangles</code> where <code>rectangles[i] = [x<sub>i</sub>, y<sub>i</sub>, a<sub>i</sub>, b<sub>i</sub>]</code> represents an axis-aligned rectangle. The bottom-left point of the rectangle is <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and the top-right point of it is <code>(a<sub>i</sub>, b<sub>i</sub>)</code>.</p>

<p>Return <code>true</code> <em>if all the rectangles together form an exact cover of a rectangular region</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/perectrec1-plane.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
<strong>Output:</strong> true
<strong>Explanation:</strong> All 5 rectangles together form an exact cover of a rectangular region.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/perfectrec2-plane.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
<strong>Output:</strong> false
<strong>Explanation:</strong> Because there is a gap between the two rectangular regions.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/27/perfecrrec4-plane.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
<strong>Output:</strong> false
<strong>Explanation:</strong> Because two of the rectangles overlap with each other.
</pre>

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

<ul>
	<li><code>1 &lt;= rectangles.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>rectangles[i].length == 4</code></li>
	<li><code>-10<sup>5</sup> &lt;= x<sub>i</sub> &lt; a<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= y<sub>i</sub> &lt; b<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>


class Event{
public:
    int x, y;
    bool isStart;
    int rectId;
    Event(int _x, int _y, bool _isStart, int _rectId)
        : x(_x), y(_y), isStart(_isStart), rectId(_rectId) {}
};

class Interval{
public:
    int low, high;
    Interval(int l, int h) : low(l), high(h){}
    bool operator < (const Interval& b) const {
        if (low == b.low) return high < b.high;
        return low < b.low;
    }
    bool operator > (const Interval& b) const {
        if (low == b.low) return high > b.high;
        return low > b.low;
    }
    bool operator == (const Interval& b) const {
        return low == b.low && high == b.high;
    }
};

class Solution {
public:
    int minX, minY, maxX, maxY;

    bool check(multiset<Interval>& st, int low, int high){
        if (st.empty()) return true;
        auto it = begin(st), jt = next(it); 
        while (jt != end(st)){
            if (jt->low != it->high) return false;
            it = jt, jt++;
        }
        return st.begin()->low == low && st.rbegin()->high == high;
    }


    bool verticalScan(vector<vector<int>>& rects, vector<Event>& events){
        multiset<Interval> intevals;
        sort(begin(events), end(events), [](const Event& a, const Event& b){
            if (a.x == b.x) return !a.isStart && b.isStart;
            return a.x < b.x;
        });
        int index = 0;
        Event e(0, 0, 0, 0);
        Interval itev(0, 0);
        while (index < events.size()){
            int curX = events[index].x;
            while (index < events.size() && events[index].x == curX){
                e = events[index];
                itev = Interval(rects[e.rectId][1], rects[e.rectId][3]);
                if (e.isStart){
                    intevals.insert(itev);
                    // cout << "Push event: " << itev.low << " - " << itev.high << endl;
                } else {
                    intevals.erase(intevals.find(itev));
                    // cout << "Pop event: " << itev.low << " - " << itev.high << endl;
                }
                index++;
            }

            if (!check(intevals, minY, maxY)) return false;
        }
        return true;
    }

    bool horizontalScan(vector<vector<int>>& rects, vector<Event>& events){
        multiset<Interval> intevals;
        sort(begin(events), end(events), [](const Event& a, const Event& b){
            if (a.y == b.y) return !a.isStart && b.isStart;
            return a.y < b.y;
        });
        
        int index = 0;
        Event e(0, 0, 0, 0);
        Interval itev(0, 0);
        while (index < events.size()){
            int curY = events[index].y;
            while (index < events.size() && events[index].y == curY){
                e = events[index];
                itev = Interval(rects[e.rectId][0], rects[e.rectId][2]);
                if (e.isStart){
                    intevals.insert(itev);
                    // cout << "Push event: " << itev.low << " - " << itev.high << endl;
                } else {
                    intevals.erase(intevals.find(itev));
                    // cout << "Pop event: " << itev.low << " - " << itev.high << endl;
                }
                index++;
            }

            if (!check(intevals, minX, maxX)) return false;
        }
        return true;
    }


    bool isRectangleCover(vector<vector<int>>& rects) {
        int n = rects.size();
        vector<Event> events;
        minX = minY = INT_MAX;
        maxX = maxY = INT_MIN;
        for (int i = 0; i < n; i++){
            events.push_back(Event(rects[i][0], rects[i][1], true, i));
            events.push_back(Event(rects[i][2], rects[i][3], false, i));
            minX = min(minX, rects[i][0]);
            minY = min(minY, rects[i][1]);
            maxX = max(maxX, rects[i][2]);
            maxY = max(maxY, rects[i][3]);
        }

        if (!verticalScan(rects, events)) return false;
        // return true;
        return horizontalScan(rects, events);
    }
};
40100