<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) {
        // Variables to store the min and max bounds of the large rectangle
        int x_min = INT_MAX, y_min = INT_MAX, x_max = INT_MIN, y_max = INT_MIN;
        long long totalArea = 0;  // Use long long for larger area calculations
        set<pair<int, int>> points;  // Set to track unique corner points
        
        for (const auto& rect : rectangles) {
            int x1 = rect[0], y1 = rect[1], x2 = rect[2], y2 = rect[3];
            
            // Update the boundaries of the large rectangle
            x_min = min(x_min, x1);
            y_min = min(y_min, y1);
            x_max = max(x_max, x2);
            y_max = max(y_max, y2);
            
            // Calculate the area of the current rectangle and add it to the total area
            long long area = static_cast<long long>(x2 - x1) * static_cast<long long>(y2 - y1);
            totalArea += area;
            
            // List of four corners of the current rectangle
            vector<pair<int, int>> corners = {{x1, y1}, {x1, y2}, {x2, y1}, {x2, y2}};
            
            // Toggle the presence of each corner in the set
            for (const auto& corner : corners) {
                if (points.count(corner)) {
                    points.erase(corner);  // If the corner is already in the set, remove it
                } else {
                    points.insert(corner);  // Otherwise, add it
                }
            }
        }
        
        // Calculate the area of the large rectangle using long long
        long long expectedArea = static_cast<long long>(x_max - x_min) * static_cast<long long>(y_max - y_min);
        
        // The total area must match the area of the large rectangle
        if (totalArea != expectedArea) return false;
        
        // The set of points should contain exactly the four corners of the large rectangle
        if (points.size() != 4 ||
            points.count({x_min, y_min}) == 0 ||
            points.count({x_min, y_max}) == 0 ||
            points.count({x_max, y_min}) == 0 ||
            points.count({x_max, y_max}) == 0) {
            return false;
        }
        
        return true;
    }
};

38700