#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100000;
struct Event
{
int x, y1, y2;
int add;
};
struct IntervalTreeNode
{
int count, total;
};
int n;
struct Rectangle
{
int x1, y1, x2, y2;
};
Rectangle rect[maxn + 1];
Event evt[maxn * 2 + 1];
IntervalTreeNode tree[(maxn + 10) << 2];
int id[maxn * 2];
bool cmp(const Event &a, const Event &b)
{
return a.x < b.x;
}
void up(int i, int lb, int rb)
{
tree[i].total = tree[i << 1].total + tree[(i << 1) + 1].total;
if (tree[i].count) tree[i].total = id[rb] - id[lb];
}
void ins(int i, int lb, int rb, int a, int b, int k)
{
if (a == lb && b == rb)
{
tree[i].count += k;
up(i, lb, rb);
return;
}
int med = (lb + rb) >> 1;
if (b <= med) ins(i << 1, lb, med, a, b, k);
else if (a >= med) ins((i << 1) + 1, med, rb, a, b, k);
else
{
ins(i << 1, lb, med, a, med, k);
ins((i << 1) + 1, med, rb, med, b, k);
}
up(i, lb, rb);
}
long long area()
{
for (int i = 0; i < n; i++)
{
id[i] = rect[i].y1;
id[i + n] = rect[i].y2;
}
sort(id, id + 2 * n);
for (int i = 0; i < 2 * n; i++)
{
rect[i].y1 = lower_bound(id, id + 2 * n, rect[i].y1) - id;
rect[i].y2 = lower_bound(id, id + 2 * n, rect[i].y2) - id;
}
for (int i = 0; i < n; i++)
{
evt[i].add = 1;
evt[i + n].add = -1;
evt[i].x = rect[i].x1;
evt[i + n].x = rect[i].x2;
evt[i].y1 = evt[i + n].y1 = rect[i].y1;
evt[i].y2 = evt[i + n].y2 = rect[i].y2;
}
sort(evt, evt + n * 2, cmp);
long long ans = 0;
for (int i = 0; i < 2 * n; i++)
{
if (i > 0 && evt[i].x > evt[i - 1].x)
ans += (long long)(evt[i].x - evt[i - 1].x) * tree[1].total;
ins(1, 0, 2 * n - 1, evt[i].y1, evt[i].y2, evt[i].add);
}
return ans;
}
int a, b, c, d;
int init()
{
int cnt = 0;
while (scanf("%d%d%d%d", &a, &b, &c, &d) == 4 && a + b + c + d != -4)
{
rect[cnt].x1 = a, rect[cnt].y1 = b;
rect[cnt].x2 = c, rect[cnt].y2 = d;
cnt++;
}
return n = cnt;
}
int main(int argc, char const *argv[])
{
while (init())
{
printf("%lld\n", area());
}
return 0;
}
线段树经典题,取出矩形的纵向边从左往右扫描,每处理一条扫描线时,下一条扫描线与当前扫描线的距离乘上当前已覆盖纵向边的长度是一个部分面积,将这些面积累加起来就是n个矩形面积的并,用线段树维护扫描覆盖纵向边长度,离散化点坐标可以节约空间,貌似不离散也能过。。。