<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:
    def get_y_area(self, active_intervals):
        if not active_intervals: return (-1, -1)
        active_intervals.sort()
        # print("active_intervals", active_intervals)
        for i in range(1, len(active_intervals)):
            y_start, y_end = active_intervals[i]
            if y_start != active_intervals[i-1][1]:
                return (-1, -1)
        return (active_intervals[0][0], active_intervals[-1][1])

    def isRectangleCover(self, rectangles: List[List[int]]) -> bool:
        events = []
        START, END = 1, 0
        for x1,y1,x2,y2 in rectangles:
            events.append((x1, START, y1, y2))
            events.append((x2, END, y1, y2))
        
        events.sort()
        # print(events)
        x_last = events[0][0]
        cur_intervals = []
        res_rectangles = []
        for x, typ, y_start, y_end in events:
            # add event
            # print("x", x, "typ", typ, "y_start", y_start, "y_end", y_end)
            if x != x_last:
                y1, y2 = self.get_y_area(cur_intervals)
                if (y1,y2) == (-1,-1): 
                    return False # overlapping found
                res_rectangles.append((x_last, y1, x, y2))
            x_last = x
            if typ == START:
                cur_intervals.append((y_start, y_end))
            else:
                cur_intervals.remove((y_start, y_end))
            
        print(res_rectangles)
        for i in range(1, len(res_rectangles)):
            last_x1, last_y1, last_x2, last_y2 = res_rectangles[i-1]
            x1, y1, x2, y2 = res_rectangles[i]
            if last_x2 != x1 or last_y1 != y1 or last_y2 != y2:
                return False
        return True

            
24000