Posters
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2569 Accepted Submission(s): 564
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
Source
Recommend
lcy
//其实这题就是求面积的并、把一个切成4个、其它就没什么了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 50003
#define lson l,m,k<<1
#define rson m,r,k<<1|1
using namespace std;
struct segment
{
__int64 len;
int cover;
};
struct line
{
int x,y1,y2;
int flag;
bool operator <(const line&L)const
{
return x<L.x;
}
};
segment st[N<<2];
line li[N*8];
int rcy[N*4];
void build(int l,int r,int k)
{
st[k].len=st[k].cover=0;
if(l+1==r)
return ;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void up(int &k,int &l,int &r)
{
if(st[k].cover)
st[k].len=rcy[r]-rcy[l];
else if(l+1==r)
st[k].len=0;
else
st[k].len=st[k<<1].len+st[k<<1|1].len;
}
int flag;
void update(int &y1,int &y2,int l,int r,int k)
{
if(y1<=rcy[l]&&rcy[r]<=y2)
{
st[k].cover+=flag;
up(k,l,r);
return ;
}
int m=(l+r)>>1;
if(y1<rcy[m]) update(y1,y2,lson);
if(y2>rcy[m]) update(y1,y2,rson);
up(k,l,r);
}
int main()
{
int n;
__int64 S;
int i,j,k,l;
int x1,x2,x3,x4,y1,y2,y3,y4;
while(scanf("%d",&n),n)
{
for(l=j=i=0;i<n;i++)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
li[j].x=x1;li[j].y1=y1;li[j].y2=y2;
li[j++].flag=1;rcy[l++]=y1;
li[j].x=x3;li[j].y1=y1;li[j].y2=y2;
li[j++].flag=-1;rcy[l++]=y2;
li[j].x=x3;li[j].y1=y1;li[j].y2=y3;
li[j++].flag=1;rcy[l++]=y3;
li[j].x=x4;li[j].y1=y1;li[j].y2=y3;
li[j++].flag=-1;rcy[l++]=y4;
li[j].x=x3;li[j].y1=y4;li[j].y2=y2;
li[j++].flag=1;
li[j].x=x4;li[j].y1=y4;li[j].y2=y2;
li[j++].flag=-1;
li[j].x=x4;li[j].y1=y1;li[j].y2=y2;
li[j++].flag=1;
li[j].x=x2;li[j].y1=y1;li[j].y2=y2;
li[j++].flag=-1;
}
sort(li,li+j);
sort(rcy,rcy+l);
for(k=0,i=1;i<l;i++)
if(rcy[i]!=rcy[k])
rcy[++k]=rcy[i];
build(0,k,1);
j--;
S=0;
for(i=0;i<j;i++)
{
if(li[i].y1!=li[i].y2)
{
flag=li[i].flag;
update(li[i].y1,li[i].y2,0,k,1);
}
S+=st[1].len*(li[i+1].x-li[i].x);//开始这个放在if里面、Wa好多次,因为在遇到洞置顶的话就会错了
}
printf("%I64d\n",S);
}
return 0;
}