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

#pragma GCC optimize("O2,unroll-loops")

namespace nsss{
    static int ns_lambda() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        std::cout.tie(0);
        std::wcin.tie(0);
        std::wcout.tie(0);
        return 0;
    };

    auto ns_obj = ns_lambda();
}

class Solution {
public:
    static int64_t area(vector<int> const& rect) {
        int64_t a = (int64_t)rect[2] - (int64_t)rect[0];
        int64_t b = (int64_t)rect[3] - (int64_t)rect[1];

        return a * b;
    }
     
    static constexpr int make_hash_element_i(int x, int y) noexcept {
        int code = 1;
        code = code * 31 + x;
        code = code * 31 + y;
        return code;
    }
  
 
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        int64_t area = 0;
        int lx = +1'000'000'000;
        int ly = +1'000'000'000;
        int rx = -1'000'000'000;
        int ry = -1'000'000'000;

        std::unordered_set<int> set;

        for (const auto& rect: rectangles) {
            const int x = rect[0];
            const int y = rect[1];
            

            const int x_2 = rect[2];
            const int y_2 = rect[3];
            
            lx = std::min(lx, x);
            ly = std::min(ly, y);

            rx = std::max(rx, x_2);
            ry = std::max(ry, y_2);

            area += (int64_t)(x_2 - x) * (y_2 - y);

            const auto s1 = make_hash_element_i(x, y);
            const auto s2 = make_hash_element_i(x, y_2);
            const auto s3 = make_hash_element_i(x_2, y_2);
            const auto s4 = make_hash_element_i(x_2, y);

            if (auto [iter, inserted] = set.insert( s1 ); !inserted ) set.erase(iter);
            if (auto [iter, inserted] = set.insert( s2 ); !inserted ) set.erase(iter);
            if (auto [iter, inserted] = set.insert( s3 ); !inserted ) set.erase(iter);
            if (auto [iter, inserted] = set.insert( s4 ); !inserted ) set.erase(iter);
        }

        const auto t1 = make_hash_element_i(lx, ly);
        const auto t2 = make_hash_element_i(lx, ry);
        const auto t3 = make_hash_element_i(rx, ly);
        const auto t4 = make_hash_element_i(rx, ry);
        if (set.find(t1) == set.end() ||
               set.find(t2) == set.end() ||
               set.find(t3) == set.end() ||
               set.find(t4) == set.end() ||
               set.size() != 4) {
                return false;
        }
        const int64_t max_area = (int64_t)(rx - lx) * (ry - ly);

        if (max_area != area){
            return false;
        }

      
          
        
        return true;


    }
};
33500