## 【hdu3265】Posters

2015.04.17 10:33 Fri | 0次阅读 | | 固定链接 | 源码

Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters.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.
Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.The input ends with a line of single zero.
Output
For each test case, output a single line with the total area of window covered by posters.
Sample Input
2 0 0 10 10 1 1 9 9 2 2 8 8 3 3 7 7 0
Sample Output
56

## 我的程序

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn 50005
#define ll long long
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
int n,x1,x2,x3,x4,y,y2,y3,y4,cnt;
ll ans;
int len[maxn<<4],cover[maxn<<4];
struct Line{
int l,r,h,flag;
}line[maxn*10];
int p[maxn*10];
void add(int x,int y,int xx,int yy)
{
line[++cnt].l=x;
line[cnt].r=xx;
line[cnt].h=y;
line[cnt].flag=1;
p[cnt]=x;
line[++cnt].l=x;
line[cnt].r=xx;
line[cnt].h=yy;
line[cnt].flag=-1;
p[cnt]=xx;
}
bool cmp(const Line &a,const Line &b)
{
return a.h<b.h||(a.h==b.h&&a.flag>b.flag);
}
void Init()
{
cnt=ans=0;
memset(len,0,sizeof(len));
memset(cover,0,sizeof(cover));
}
void pushup(int l,int r,int rt)
{
if(cover[rt]) len[rt]=p[r+1]-p[l];
else if(l==r) len[rt]=0;
else len[rt]=len[rt<<1]+len[rt<<1|1];
}
void update(int l,int r,int rt,int L,int R,int flag)
{
if(L>R) return;
if(L<=l&&R>=r)
{
cover[rt]+=flag;
pushup(l,r,rt);
return;
}
int mid=(l+r)>>1;
if(mid>=L) update(lson,L,R,flag);
if(R>mid) update(rson,L,R,flag);
pushup(l,r,rt);
}
int main()
{
while(cin>>n&&n)
{
Init();
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y,&x2,&y2,&x3,&y3,&x4,&y4);
}
sort(p+1,p+cnt+1);
sort(line+1,line+cnt+1,cmp);
int m=1;
for(int i=2;i<=cnt;i++)
if(p[i]!=p[i-1]) p[++m]=p[i];
for(int l,r,i=1;i<=cnt;i++)
{
l=lower_bound(p+1,p+m+1,line[i].l)-p;
r=lower_bound(p+1,p+m+1,line[i].r)-p;
update(1,m-1,1,l,r-1,line[i].flag);
ans+=(ll)(line[i+1].h-line[i].h)*len[1];
}
printf("%I64d\n",ans);
}
return 0;
}
`