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

/******** Debug Code *******/
#include <bits/stdc++.h>
#define cerr cout
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename A>void __print(const A &x);
template <typename A, typename B>void __print(const pair<A, B> &p);
template <typename... A>void __print(const tuple<A...> &t);
template <typename T>void __print(stack<T> s);
template <typename T>void __print(queue<T> q);
template <typename T, typename... U>void __print(priority_queue<T, U...> q);
template <typename A>void __print(const A &x) {bool first = true;cerr << '{';for (const auto &i : x) {    cerr << (first ? "" : ","), __print(i);    first = false;}cerr << '}';}
template <typename A, typename B>void __print(const pair<A, B> &p) {cerr << '(';__print(p.first);cerr << ',';__print(p.second);cerr << ')';}
template <typename... A>void __print(const tuple<A...> &t) {bool first = true;cerr << '(';apply([&first](const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);cerr << ')';}
template <typename T>void __print(stack<T> s) {vector<T> debugVector;while (!s.empty()) {    T t = s.top();    debugVector.push_back(t);    s.pop();}reverse(debugVector.begin(), debugVector.end());__print(debugVector);}
template <typename T>void __print(queue<T> q) {vector<T> debugVector;while (!q.empty()) {    T t = q.front();    debugVector.push_back(t);    q.pop();}__print(debugVector);}
template <typename T, typename... U>void __print(priority_queue<T, U...> q) {vector<T> debugVector;while (!q.empty()) {    T t = q.top();    debugVector.push_back(t);    q.pop();}__print(debugVector);}void _print() { cerr << "]\n"; }
template <typename Head, typename... Tail>void _print(const Head &H, const Tail &...T) {__print(H);if (sizeof...(T))    cerr << ", ";_print(T...);}
#define debug(x...) //cerr << "[" << #x << "] = ["; _print(x)

class Solution {
public:
    int rectangleArea(vector<vector<int>>& a) {
        const int mod = 1e9 + 7;
        // chia ra các khoảng theo Oy -> tối đa 400 khoảng
        set<int> st;
        for(auto&x:a) {
            st.insert(x[1]);
            st.insert(x[3]);
        }
        vector<int> b;
        for(auto&x:st) b.push_back(x);
        debug(b);
        // For từng khoảng, duyetj hết các khối để đánh dáu
        int res = 0;
        for(int i=0; i<b.size()-1; i++) {
            int dy = b[i+1] - b[i];
            map<int,int> mp;
            for(auto&x:a) {
                if(x[3] <= b[i] || x[1]>=b[i+1]) continue;
                mp[x[0]]++;
                mp[x[2]]--;
            }
            int sum = 0;
            for(auto&[x,y]:mp) {
                sum += y;
                mp[x] = sum;
            }
            int fi = INT_MAX;
            for(auto&[x,y]:mp) {
                if(y==0) {
                    if(fi!=INT_MAX) res = (res+(1ll*(x-fi)*dy)%mod)%mod;
                    // debug(x-fi, dy);
                    fi = INT_MAX;
                } else {
                    fi = min(fi, x);
                }
            }
            debug(b[i], b[i+1], mp, res);
        }
        // duyệt lại xem đánh dấu được bao nhiêu x  _. nhân với khoảng là ra dienj tích khoảng này
        return res;
    }
};
18400