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

namespace std {
    template <>
    struct hash<pair<int, int>> {
        size_t operator()(const pair<int, int>& p) const {
            return hash<int>{}(p.first) ^ (hash<int>{}(p.second) << 1); // Combine the two hash values
        }
    };
}

class Solution {
public:
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        if (rectangles.size() == 1) return true;

        unordered_map<pair<int, int>, pair<int, int>, std::hash<pair<int, int>>> recs;
        int limleft = numeric_limits<int>::max();
        int limbot = numeric_limits<int>::max();
        int width = 0, height = 0, areasum = 0;

        
        for (const auto& rec : rectangles) {
            limleft = min(limleft, rec[0]);
        }
        for (const auto& rec : rectangles) {
            if (rec[0] == limleft) {
                limbot = min(limbot, rec[1]);
            }
        }

        
        for (const auto& rec : rectangles) {
            auto corner = make_pair(rec[0], rec[1]);
            if (recs.count(corner)) return false;
            recs[corner] = make_pair(rec[2], rec[3]);
        }

        
        pair<int, int> up(limleft, limbot), right(limleft, limbot);
        while (recs.count(up) || recs.count(right)) {
            if (recs.count(up)) {
                height += recs[up].second - up.second;
                up = {limleft, recs[up].second};
            }
            if (recs.count(right)) {
                width += recs[right].first - right.first;
                right = {recs[right].first, limbot};
            }
        }

        int limright = limleft + width, limtop = limbot + height;
        stack<pair<int, int>> squaresfilter;
        squaresfilter.push({limleft, limbot});

        
        while (!squaresfilter.empty()) {
            auto curr = squaresfilter.top();
            squaresfilter.pop();
            if (recs.count(curr)) {
                if (curr.first < limleft || curr.second < limbot || recs[curr].first > limright || recs[curr].second > limtop) {
                    return false;
                }
                areasum += (recs[curr].first - curr.first) * (recs[curr].second - curr.second);
                squaresfilter.push({curr.first, recs[curr].second});
                squaresfilter.push({recs[curr].first, curr.second});
                recs.erase(curr);
            }
        }

        return areasum == width * height && recs.empty() && areasum != 0;
    }
};
32800