<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 Solution {
public:

    struct Node {
        int x;
        int y1;
        int y2;
        int sign;
    };

    static bool mycmp(Node a, Node b) {
        if (a.x != b.x) return a.x < b.x;
        if (a.sign != b.sign) return a.sign < b.sign;
        return a.y1 < b.y1;
    }

    bool intersect(int l1, int r1, int l2, int r2) {
        int L = max(l1, l2), R = min(r1, r2);
        return L < R;
    }

    bool isRectangleCover(vector<vector<int>> rectangles) {
        int minX = 1e9, minY = 1e9, maxX = -1e9, maxY = -1e9;
        vector<Node> a, b;
        long long sum = 0;
        for (const auto& r : rectangles) {
            int x1 = r[0], y1 = r[1], x2 = r[2], y2 = r[3];
            sum += 1ll * (x2 - x1) * (y2 - y1);
            minX = min(minX, x1);
            minY = min(minY, y1);

            maxX = max(maxX, x2);
            maxY = max(maxY, y2);
            Node left, right;
            left.x = x1, left.y1 = y1, left.y2 = y2, left.sign = 1;
            right.x = x2, right.y1 = y1, right.y2 = y2, right.sign = -1;
            a.push_back(left), a.push_back(right);
            Node left2, right2;
            left2.x = y1, left2.y1 = x1, left2.y2 = x2, left2.sign = 1;
            right2.x = y2, right2.y1 = x1, right2.y2 = x2, right2.sign = -1;
            b.push_back(left2), b.push_back(right2);
        }
        sort(a.begin(), a.end(), mycmp);
        sort(b.begin(), b.end(), mycmp);
        int Y = maxY - minY, i = 0, X = maxX - minX;
        if (1ll * sum != 1ll * (maxX - minX) * (maxY - minY)) return false;
        long long diff = 0;
        while (i < a.size()) {
            int j = i;
            while (j < a.size() && a[i].x == a[j].x) ++j; --j;
            vector<pair<int, int> > intervals1, intervals2;
            for (int k = i; k <= j; ++k) {
                diff += 1ll * (a[k].y2 - a[k].y1) * a[k].sign;
                if (a[k].sign == 1) intervals1.push_back({a[k].y1, a[k].y2}); else
                                    intervals2.push_back({a[k].y1, a[k].y2});
            }
            for (int k = 0; k + 1 < intervals1.size(); ++k) {
                if (intersect(intervals1[k].first, intervals1[k].second, 
                              intervals1[k + 1].first, intervals1[k + 1].second)) return false;
            }
            for (int k = 0; k + 1 < intervals2.size(); ++k) {
                if (intersect(intervals2[k].first, intervals2[k].second, 
                              intervals2[k + 1].first, intervals2[k + 1].second)) return false;
            }
            if (j + 1 == a.size()) break;
            if (diff != Y) return false;
            i = j + 1;
        }
        return true;
    }
};
44900