<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:
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        int X1 = numeric_limits<int>::max(), Y1 = numeric_limits<int>::max();
        int X2 = numeric_limits<int>::min(), Y2 = numeric_limits<int>::min();
        // 记录所有小矩形的面积之和
        long long actualArea = 0;
        unordered_set<string> corners;

        for (vector<int>& rect : rectangles) {
            int x1 = rect[0], y1 = rect[1], x2 = rect[2], y2 = rect[3];
            // 计算完美矩形的理论坐标
            X1 = min(X1, x1);
            Y1 = min(Y1, y1);
            X2 = max(X2, x2);
            Y2 = max(Y2, y2);
            // 累加所有小矩形的面积
            actualArea += (long long)(x2 - x1) * (y2 - y1);

            // 处理角点
            vector<string> rectCorners = {to_string(x1) + "," + to_string(y1), to_string(x1) + "," + to_string(y2),
                                          to_string(x2) + "," + to_string(y1), to_string(x2) + "," + to_string(y2)};
            for (string& corner : rectCorners) {
                if (corners.count(corner)) {
                    corners.erase(corner);
                } else {
                    corners.insert(corner);
                }
            }
        }

        // 计算完美矩形的理论面积
        long long expectedArea = (long long)(X2 - X1) * (Y2 - Y1);
        // 面积应该相同
        if (actualArea != expectedArea) {
            return false;
        }

        // 最终角点集合应该只包含完美矩形的四个角点
        vector<string> expectedCorners = {to_string(X1) + "," + to_string(Y1), to_string(X1) + "," + to_string(Y2),
                                          to_string(X2) + "," + to_string(Y1), to_string(X2) + "," + to_string(Y2)};
        for (string& corner : expectedCorners) {
            if (!corners.count(corner)) {
                return false;
            }
            corners.erase(corner);
        }

        return corners.empty();
    }
};
48400