线段树求面积的并,虽然每个矩形有个孔,但我们可以把他分成四个矩形,然后就.....
环境不支持__int64,敲完编译才发现==
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
#define N 50005
#define __int64 long long
inline int input()
{
bool IsN=0;
int ret=0;
char c;
c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') IsN=1;
c=getchar();
}
while(c>='0'&&c<='9')
{
ret=ret*10+c-'0';
c=getchar();
}
return IsN?-ret:ret;
}
map<int,int>m;
int n,x,y,x2,y2,x3,y3,x4,y4,k,num,X[N];
int h[N];
struct Line
{
int s,t,y;
int cov;
}l[N*8];
bool cmp(Line i,Line j)
{
return i.y<j.y;
}
struct node
{
int l,r,cov;
__int64 len;
}root[50000*10];
inline void build(int t,int x,int y)
{
root[t].l=x;
root[t].r=y;
root[t].len=root[t].cov=0;
if(x+1==y) return;
int m=(x+y)>>1;
build(t*2,x,m);
build(t*2+1,m,y);
}
inline void Push_up(int t)
{
if(root[t].cov>0) root[t].len=h[root[t].r]-h[root[t].l];
else if(root[t].l+1==root[t].r) root[t].len=0;
else root[t].len=root[t*2].len+root[t*2+1].len;
}
inline void Modefiy(int t,int x,int y,int cov)
{
int l=root[t].l;
int r=root[t].r;
if(r<x||l>y) return;
if(x<=l&&r<=y)
{
root[t].cov+=cov;
Push_up(t);
return;
}
Modefiy(t*2,x,y,cov);
Modefiy(t*2+1,x,y,cov);
Push_up(t);
}
int main()
{
while(scanf("%d",&n)&&n)
{
m.clear();
// h.clear();
k=1;
num=0;
for(int i=1;i<=n;i++)
{
x=input(),y=input(),x2=input(),y2=input(),x3=input(),y3=input(),x4=input(),y4=input();
l[num].s=x,l[num].t=x4,l[num].y=y,l[num++].cov=1;
l[num].s=x,l[num].t=x4,l[num].y=y3,l[num++].cov=-1;
l[num].s=x4,l[num].t=x2,l[num].y=y,l[num++].cov=1;
l[num].s=x4,l[num].t=x2,l[num].y=y4,l[num++].cov=-1;
l[num].s=x3,l[num].t=x2,l[num].y=y4,l[num++].cov=1;
l[num].s=x3,l[num].t=x2,l[num].y=y2,l[num++].cov=-1;
l[num].s=x,l[num].t=x3,l[num].y=y3,l[num++].cov=1;
l[num].s=x,l[num].t=x3,l[num].y=y2,l[num++].cov=-1;
m[x]=1;
m[x2]=1;
m[x3]=1;
m[x4]=1;
}
map<int,int>::iterator it=m.begin();
for(;it!=m.end();++it)
{
X[(*it).first]=k;
h[k]=(*it).first;
k++;
}
sort(l,l+num,cmp);
build(1,1,k);
__int64 ans=0;
for(int i=0;i<num-1;i++)
{
int a=X[l[i].s];
int b=X[l[i].t];
if(a!=b) Modefiy(1,a,b,l[i].cov);
ans+=root[1].len*(l[i+1].y-l[i].y);
}
printf("%I64d\n",ans);
}
return 0;
}