Posters
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5230 Accepted Submission(s): 1220
However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window.
Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.
To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes.
The input ends with a line of single zero.
0
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 50005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} struct node{
int l, r;
int val;
__int64 sum;
}a[N*]; struct Line{
int x1, x2, y;
int val;
Line(){}
Line(int a,int b,int c,int d){
x1=a;
x2=b;
y=c;
val=d;
}
}line[N*]; bool cmp(Line a,Line b){
return a.y<b.y;
} int xx[N*];
int n, m; int b_s(int key){
int l=, r=m;
while(l<=r){
int mm=(l+r)/;
if(xx[mm]==key) return mm;
else if(xx[mm]<key) l=mm+;
else if(xx[mm]>key) r=mm-;
}
} void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].sum=a[root].val=;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
} void up(int root){
if(a[root].val) a[root].sum=xx[a[root].r+]-xx[a[root].l];
else if(a[root].l==a[root].r) a[root].sum=;
else a[root].sum=a[ll].sum+a[rr].sum;
} void update(int l,int r,int val,int root){
if(l>r) return;
if(a[root].l==l&&a[root].r==r){
a[root].val+=val;
up(root);
return;
}
if(r<=a[ll].r) update(l,r,val,ll);
else if(l>=a[rr].l) update(l,r,val,rr);
else{
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
up(root);
}
main()
{
int x1, y1, x2, y2, x3, y3, x4, y4;
int i, j, k;
while(scanf("%d",&n)&&n){
k=;
m=;
for(i=;i<n;i++){
scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
line[k++]=Line(x1,x3,y1,);
line[k++]=Line(x1,x3,y2,-);
line[k++]=Line(x3,x4,y1,);
line[k++]=Line(x3,x4,y3,-);
line[k++]=Line(x3,x4,y4,);
line[k++]=Line(x3,x4,y2,-);
line[k++]=Line(x4,x2,y1,);
line[k++]=Line(x4,x2,y2,-);
xx[m++]=x1;
xx[m++]=x2;
xx[m++]=x3;
xx[m++]=x4;
}
sort(xx+,xx+m);
m=unique(xx+,xx+m)-xx-;
sort(line,line+k,cmp);
build(,m,);
__int64 ans=;
for(i=;i<k-;i++){
update(b_s(line[i].x1),b_s(line[i].x2)-,line[i].val,);
ans+=a[].sum*(__int64)(line[i+].y-line[i].y);
}
printf("%I64d\n",ans);
}
}