<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>

#include <vector>
#include <map>
#include <utility> // For std::pair
#include <algorithm> // For std::min and std::max

using namespace std;

class Solution {
public:
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        long long area = 0; // Use long long to avoid overflow
        long long minX = rectangles[0][0], minY = rectangles[0][1];
        long long maxX = rectangles[0][2], maxY = rectangles[0][3];

        using pii = pair<long long, long long>; // Use long long for pairs
        map<pii, int> cnt;

        for (auto& r : rectangles) {
            // Calculate area and update the total area
            area += (long long)(r[2] - r[0]) * (r[3] - r[1]);

            // Update the min and max coordinates
            minX = min(minX, (long long)r[0]);
            minY = min(minY, (long long)r[1]);
            maxX = max(maxX, (long long)r[2]);
            maxY = max(maxY, (long long)r[3]);

            // Count the corners
            ++cnt[{(long long)r[0], (long long)r[1]}];
            ++cnt[{(long long)r[0], (long long)r[3]}];
            ++cnt[{(long long)r[2], (long long)r[3]}];
            ++cnt[{(long long)r[2], (long long)r[1]}];
        }

        // Check if the area matches and the corners are correct
        if (area != (maxX - minX) * (maxY - minY) || 
            cnt[{minX, minY}] != 1 || 
            cnt[{minX, maxY}] != 1 || 
            cnt[{maxX, maxY}] != 1 || 
            cnt[{maxX, minY}] != 1) {
            return false;
        }

        // Remove the corners from the count
        cnt.erase({minX, minY});
        cnt.erase({minX, maxY});
        cnt.erase({maxX, maxY});
        cnt.erase({maxX, minY});

        // Check remaining corners
        return all_of(cnt.begin(), cnt.end(), [](pair<pii, int> e) {
            return e.second == 2 || e.second == 4;
        });
    }
};
38800