题源:https://www.luogu.org/problem/P5490
#include <bits/stdc++.h>
#define numm ch - 48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define fi first
#define se second
#define fre1 freopen("1.txt", "r", stdin)
#define fre2 freopen("2.txt", "w", stdout)
typedef long long int ll;
typedef long long int LL;
using namespace std;
template<typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = numm; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + numm);
flag && (res = -res);
}
template<typename T>
void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
//static auto _ = []() {
// ios::sync_with_stdio(false);
// cin.tie(0);
// return 0;
//}();
//#pragma GCC optimize(3,"Ofast","inline")
//////////////////////////////////////////////////////////////////////////////////////////////
const int maxn = 1e6 + 5;
ll n, x, y, xx, yy, cnt = 0;
ll Y[maxn << 1];
struct node {
ll x, y, yy;
int flag;
} scan[maxn << 1];
bool cmp(node a, node b) {
return a.x < b.x;
}
struct tree {
int l, r, sum;
ll len;
} tr[maxn << 2];
void build(int o, int l, int r) {
tr[o].l = l, tr[o].r = r;
tr[o].len = tr[o].sum = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(o << 1, l, mid);
build(o << 1 | 1, mid + 1, r);
}
void push(int o) {
int l = tr[o].l, r = tr[o].r;
if (tr[o].sum) tr[o].len = Y[r + 1] - Y[l];
else tr[o].len = tr[o << 1].len + tr[o << 1 | 1].len;
}
void change(int o, ll L, ll R, int c) {
int l = tr[o].l, r = tr[o].r;
if (R <= Y[l] || Y[r + 1] <= L) return;
if (L <= Y[l] && Y[r + 1] <= R) {
tr[o].sum += c;
push(o);
return;
}
change(o << 1, L, R, c);
change(o << 1 | 1, L, R, c);
push(o);
}
int main() {
read(n);
for (int i = 1; i <= n; i++) {
read(x), read(y), read(xx), read(yy);
scan[++cnt].x = x;
scan[cnt].y = y;
scan[cnt].yy = yy;
scan[cnt].flag = 1;
Y[cnt] = y;
scan[++cnt].x = xx;
scan[cnt].y = y;
scan[cnt].yy = yy;
scan[cnt].flag = -1;
Y[cnt] = yy;
}
sort(scan + 1, scan + cnt + 1, cmp);
sort(Y + 1, Y + cnt + 1);
int tot = unique(Y + 1, Y + cnt + 1) - (Y + 1);
build(1, 1, tot - 1);
ll ans = 0;
for (int i = 1; i < cnt; i++) {
change(1, scan[i].y, scan[i].yy, scan[i].flag);
ans += tr[1].len *(scan[i + 1].x - scan[i].x);
}
write(ans);
return 0;
}