以下是一份求矩形面积并的代码。(所有坐标皆为非负整数)
为啥 MLE 了捏。(内存限制128MB)
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int ls(int x) { return x << 1; }
int rs(int x) { return x << 1 | 1; }
int n, tot, sum[N<<2], len[N<<2], X[N<<1];
struct L { int l, r, h, mark; }line[N<<1];
bool cmp(L a, L b) { return a.h < b.h; }
void pushup(int x, int l, int r)
{
if (sum[x]) len[x] = X[r + 1] - X[l];
else if (l == r) len[x] = 0;
else len[x] = len[ls(x)] + len[rs(x)];
}
void update(int x, int l, int r, int L, int R, int c)
{
if (X[r + 1] <= L || R <= X[l]) return ;
if (L <= X[l] && X[r + 1] <= R) { sum[x] += c, pushup(x, l, r); return ; }
int mid = (l + r) >> 1;
update(ls(x), l, mid, L, R, c), update(rs(x), mid + 1, r, L, R, c);
pushup(x, l, r);
}
int main()
{
scanf("%d", &n); long long ret = 0;
for (int i = 1, ax, ay, bx, by; i <= n; i++)
{
scanf("%d%d%d%d", &ax, &ay, &bx, &by), X[2*i-1] = ax, X[2*i] = bx;
line[2*i-1] = {ax, bx, ay, 1}, line[2*i] = {ax, bx, by, -1};
} n <<= 1, sort(line + 1, line + n + 1, cmp), sort(X + 1, X + n + 1);
tot = unique(X + 1, X + n + 1) - X - 1;
for (int i = 1; i < n; i++)
{
update(1, 1, tot - 1, line[i].l, line[i].r, line[i].mark);
ret += 1LL * len[1] * (line[i + 1].h - line[i].h);
} printf("%lld\n", ret);
return 0;
}