<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:
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        unordered_map<long long, int>corner_count;
        unordered_map<long long, int>appeared;
        // int total_corner_count=0;
        // int idx=0;
        for(auto rectangle:rectangles){
            // cout<<"idx is:"<<idx<<endl;
            int x1=rectangle[0], y1=rectangle[1], x2=rectangle[2], y2=rectangle[3];
            // corner1
            long long coor=1LL*x1*1e6+y1;
            if(appeared.count(coor)&&appeared[coor]==0){
                // cout<<"corner1"<<endl;
                return false;
            }
            appeared[coor]=0;
            if(!corner_count.count(coor)){
                corner_count[coor]=1;
            }
            else {
                int corner1=corner_count[coor];
                if(corner1==14||corner1==2||corner1==8){
                    corner_count.erase(coor);
                }
                else{
                    corner_count[coor]+=1;
                }
            }
            // corner2
            coor=1LL*x1*1e6+y2;
            if(appeared.count(coor)&&appeared[coor]==1){
                // cout<<"corner2"<<endl;
                return false;
            }
            appeared[coor]=1;
            if(!corner_count.count(coor)){
                corner_count[coor]=2;
            }
            else {
                int corner2=corner_count[coor];
                if(corner2==13||corner2==1||corner2==4){
                    corner_count.erase(coor);
                }
                else{
                    corner_count[coor]+=2;

                }
            }
            // corner3
            coor=1LL*x2*1e6+y2;
            if(appeared.count(coor)&&appeared[coor]==2){
                // cout<<"corner3"<<endl;
                return false;
            }
            appeared[coor]=2;
            if(!corner_count.count(coor)){
                corner_count[coor]=4;
            }
            else {
                int corner3=corner_count[coor];
                if(corner3==11||corner3==2||corner3==8){
                    corner_count.erase(coor);
                }
                else{

                    corner_count[coor]+=4;
                }
            }
            // corner4
            coor=1LL*x2*1e6+y1;
            if(appeared.count(coor)&&appeared[coor]==3){
                // cout<<"corner4"<<endl;
                return false;
            }
            appeared[coor]=3;
            if(!corner_count.count(coor)){
                corner_count[coor]=8;
                // total_corner_count+=1;
            }
            else {
                int corner4=corner_count[coor];
                if(corner4==7||corner4==4||corner4==1){
                    corner_count.erase(coor);
                }
                else{
                    corner_count[coor]+=8;

                }
            }
            // idx+=1;
            // cout<<"number of corners are"<<corner_count.size()<<endl;
        }
        // cout<<corner_count.size()<<endl;
        return corner_count.size()==4;
    }
};
44000