<p>You are given a 2D array of axis-aligned <code>rectangles</code>. Each <code>rectangle[i] = [x<sub>i1</sub>, y<sub>i1</sub>, x<sub>i2</sub>, y<sub>i2</sub>]</code> denotes the <code>i<sup>th</sup></code> rectangle where <code>(x<sub>i1</sub>, y<sub>i1</sub>)</code> are the coordinates of the <strong>bottom-left corner</strong>, and <code>(x<sub>i2</sub>, y<sub>i2</sub>)</code> are the coordinates of the <strong>top-right corner</strong>.</p>

<p>Calculate the <strong>total area</strong> covered by all <code>rectangles</code> in the plane. Any area covered by two or more rectangles should only be counted <strong>once</strong>.</p>

<p>Return <em>the <strong>total area</strong></em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/06/rectangle_area_ii_pic.png" style="width: 600px; height: 450px;" />
<pre>
<strong>Input:</strong> rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> A total area of 6 is covered by all three rectangles, as illustrated in the picture.
From (1,1) to (2,2), the green and red rectangles overlap.
From (1,0) to (2,3), all three rectangles overlap.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> rectangles = [[0,0,1000000000,1000000000]]
<strong>Output:</strong> 49
<strong>Explanation:</strong> The answer is 10<sup>18</sup> modulo (10<sup>9</sup> + 7), which is 49.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= rectangles.length &lt;= 200</code></li>
	<li><code>rectanges[i].length == 4</code></li>
	<li><code>0 &lt;= x<sub>i1</sub>, y<sub>i1</sub>, x<sub>i2</sub>, y<sub>i2</sub> &lt;= 10<sup>9</sup></code></li>
	<li><code>x<sub>i1 &lt;= </sub>x<sub>i2</sub></code></li>
	<li><code>y<sub>i1 &lt;=</sub> y<sub>i2</sub></code></li>
</ul>

#define lli long long
lli mod=1e9+7;
class Solution {
public:
    int rectangleArea(vector<vector<int>>& v) {
        vector<vector<int>>v1(v.size(),vector<int>(5));
        set<int>st;
        for(int i=0;i<v.size();i++){
            v1[i][0]=v[i][3]-v[i][1];
            v1[i][1]=v[i][0];
            v1[i][2]=v[i][1];
            v1[i][3]=v[i][2];
            v1[i][4]=v[i][3];
            st.insert(v[i][0]);
            st.insert(v[i][2]);
        }
        sort(v1.rbegin(),v1.rend());
        vector<int>v2;
        for(auto it:st){
            v2.push_back(it);
        }
        vector<set<pair<int,int>>>vp(v2.size());
        for(int i=0;i<v1.size();i++){
            int y1=v1[i][2],y2=v1[i][4];
            //cout<<v1[i][1]<<" "<<v1[i][2]<<" "<<v1[i][3]<<" "<<v1[i][4]<<endl;
            for(int j=0;j<v2.size();j++){
                if(v2[j]>=v1[i][1]&&v2[j]<v1[i][3]){
                    //cout<<v2[j]<<endl;
                    if(vp[j].size()==0){
                        vp[j].insert({y1,y2});
                        continue;
                    }
                    for(auto it=vp[j].begin();it!=vp[j].end();it++){
                        auto it1=it;
                        it1++;
                        if(y1>=(*it).first&&y2<=(*it).second){
                            break;
                        }
                        if(y1<(*it).first&&y2>=(*it).first&&y2<=(*it).second){
                            int x=(*it).second;
                            vp[j].erase(it);
                            vp[j].insert({y1,x});
                            break;
                        }
                        if(y2<(*it).first){
                            vp[j].insert({y1,y2});
                            break;
                        }
                        if(it1==vp[j].end()){
                            if(y1<=(*it).second&&y2>(*it).second){
                                int k=(*it).first;
                                vp[j].erase(it);
                                vp[j].insert({k,y2});
                            }
                            else if(y1>(*it).second){
                                vp[j].insert({y1,y2});
                            }
                            break;
                        }
                        else{
                            if(y1>(*it).second&&y2<(*it1).first){
                                vp[j].insert({y1,y2});
                                break;
                            }
                            if(y1>=(*it).first&&y1<=(*it).second&&y2>(*it).second){
                                if(y2<(*it1).first){
                                    int x=(*it).first;
                                    vp[j].erase(it);
                                    vp[j].insert({x,y2});
                                    break;
                                }
                                else{
                                    int x1=(*it).first;
                                    int x2=max((*it).second,(*it1).second);
                                    vp[j].erase(it);
                                    vp[j].erase(it1);
                                    vp[j].insert({x1,x2});
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        lli ans=0;
        for(lli i=0;i<vp.size()-1;i++){
            lli x1=v2[i],x2=v2[i+1];
            lli t=(x2-x1);
            //cout<<x1<<endl;
            for(auto it:vp[i]){
                ans+=(t%mod*(it.second-it.first)%mod)%mod;
                ans=ans%mod;
                //cout<<it.first<<" "<<it.second<<endl;
            }
        }
        return (int)(ans);
    }
};
12800