Posters
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2312 Accepted Submission(s): 515
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
2009 Asia Ningbo Regional Contest Hosted by NIT
Recommend
lcy
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3265
题意:又是贴东西,而这回在矩形里剪掉一个矩形,问最后的图形的面积。。。
分析:其实也就是简单地把每个矩形看成四个矩形就可以了,其他的就是求矩形面积并了。。。
这题可能出现刚好剪掉的矩形在边界上,这个得判断掉,要不就re了,我居然这样re了一早上。。。最近为什么老是遇到re囧,还有杭电要用__int64?我居然忘了
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,ls
#define rson m,r,rs
using namespace std;
const int mm=222222;
const int mn=mm<<2;
struct seg
{
int x,y1,y2,c;
}g[mn];
int L,R,val,len[mn],t[mn],y[mm];
void build()
{
memset(len,0,sizeof(len));
memset(t,0,sizeof(t));
}
void updata(int l,int r,int rt)
{
if(L<=y[l]&&R>=y[r])t[rt]+=val;
else
{
int m=(l+r)>>1;
if(L<y[m])updata(lson);
if(R>y[m])updata(rson);
}
if(t[rt])len[rt]=y[r]-y[l];
else if(l>=r)len[rt]=0;
else len[rt]=len[ls]+len[rs];
}
bool cmp(seg a,seg b)
{
return a.x<b.x;
}
int main()
{
int i,n,m,x1,y1,x2,y2,x3,y3,x4,y4;
__int64 ans;
while(scanf("%d",&n),n)
{
for(i=0;i<n;++i)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
y[i]=y1,y[i+n]=y2,y[i+n*2]=y3,y[i+n*3]=y4;
g[i].x=x1,g[i].y1=y1,g[i].y2=y2,g[i].c=1;
g[i+n].x=x3,g[i+n].y1=y1,g[i+n].y2=y2,g[i+n].c=-1;
g[i+n*2].x=x3,g[i+n*2].y1=y1,g[i+n*2].y2=y3,g[i+n*2].c=1;
g[i+n*3].x=x4,g[i+n*3].y1=y1,g[i+n*3].y2=y3,g[i+n*3].c=-1;
g[i+n*4].x=x3,g[i+n*4].y1=y4,g[i+n*4].y2=y2,g[i+n*4].c=1;
g[i+n*5].x=x4,g[i+n*5].y1=y4,g[i+n*5].y2=y2,g[i+n*5].c=-1;
g[i+n*6].x=x4,g[i+n*6].y1=y1,g[i+n*6].y2=y2,g[i+n*6].c=1;
g[i+n*7].x=x2,g[i+n*7].y1=y1,g[i+n*7].y2=y2,g[i+n*7].c=-1;
}
sort(y,y+n*4);
sort(g,g+n*8,cmp);
for(m=i=0;i<n*4;++i)
if(y[m]!=y[i])y[++m]=y[i];
build();
for(ans=i=0;i<n*8;++i)
{
L=g[i].y1,R=g[i].y2,val=g[i].c;
if(L<R)updata(0,m,1);
if(g[i].x<g[i+1].x)ans+=(__int64)(g[i+1].x-g[i].x)*(__int64)len[1];
}
printf("%I64d\n",ans);
}
return 0;
}