<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:
    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 bool intersected(vector<int> const & ra, vector<int> const & rb) {
        int lxa = ra[0];
        int lya = ra[1];
        int rxa = ra[2];
        int rya = ra[3];

        int lxb = rb[0];
        int lyb = rb[1];
        int rxb = rb[2];
        int ryb = rb[3];

        int lx = std::max(lxa, lxb);
        int rx = std::min(rxa, rxb);

        int ly = std::max(lya, lyb);
        int ry = std::min(rya, ryb);

        return (lx < rx) && (ly < ry);
    }
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        const int64_t total_area = std::accumulate(rectangles.begin(),  rectangles.end(), int64_t(0), [](int64_t s, const auto & rect){ return s + area(rect);});
        int64_t lx = +1'000'000'000;
        int64_t ly = +1'000'000'000;
        int64_t rx = -1'000'000'000;
        int64_t ry = -1'000'000'000;

        for (const auto& rect: rectangles) {
            int64_t x = rect[0];
            int64_t y = rect[1];
            lx = std::min(lx, x);
            ly = std::min(ly, y);

            x = rect[2];
            y = rect[3];
            rx = std::max(x, rx);
            ry = std::max(y, ry);
        }

        const int64_t max_area = (rx - lx) * (ry - ly);

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

         const int n = (int)rectangles.size();
         for (int i = 0; i + 1 < n; ++i){
            if (intersected(rectangles[i], rectangles[i+1])) {
                return false;
            }
        }

        std::sort(rectangles.begin(), rectangles.end(), [](const auto&ra, const auto& rb) {
            return std::make_tuple(ra[0], ra[1], ra[2], ra[3]) < std::make_tuple(rb[0], rb[1], rb[2], rb[3]);
        });

       
        

        for (int i = 0; i + 1 < n; ++i){
            if (intersected(rectangles[i], rectangles[i+1])) {
                return false;
            }
        }


        std::sort(rectangles.begin(), rectangles.end(), [](const auto&ra, const auto& rb) {
            return std::make_tuple(ra[1], ra[0], ra[3], ra[2]) < std::make_tuple(rb[1], rb[0], rb[3], rb[2]);
        });

        for (int i = 0; i + 1 < n; ++i){
            if (intersected(rectangles[i], rectangles[i+1])) {
                return false;
            }
        }

        
        return true;


    }
};
28300