您的位置:首页 > 其它

HDU3265 Posters(线段树,扫描线,矩形面积并)

2017-08-07 22:25 447 查看
题目:


Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6347    Accepted Submission(s): 1527


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

 

Source

2009 Asia Ningbo Regional Contest Hosted by NIT

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3268 3264 3262 3269 3263 

 

思路:

题意是有很多矩形,每一个矩形挖去其中的一个小矩形,问最后的总面积是多少。



比如上图,红色的部分是被挖去的,我们只需要计算一下剩下颜色的四个小矩形的面积之和就可以了。

代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 50200
#define ll long long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
struct Seg
{
int l,r,h;
int f;
Seg() {}
Seg(int a,int b,int c,int d):l(a),r(b),h(c),f(d) {}
bool operator < (const Seg &cmp) const
{
return h<cmp.h;
}
} e[N<<3];
struct node
{
int cnt;
ll len;
} t[N<<2];
int X[N<<3];
void pushdown(int l,int r,int rt)
{
if(t[rt].cnt)
t[rt].len=X[r+1]-X[l];
else if(l==r)
t[rt].len=0;
else
t[rt].len=t[rt<<1].len+t[rt<<1|1].len;
}
void update(int L,int R,int l,int r,int rt,int val)
{
if(L>R) return;//重要,要排除这种情况
if(L<=l&&r<=R)
{
t[rt].cnt+=val;
pushdown(l,r,rt);
return;
}
int m=(l+r)>>1;
if(L<=m) update(L,R,lson,val);
if(R>m) update(L,R,rson,val);
pushdown(l,r,rt);
}
int main()
{
int n,q;
int a1,b1,c1,d1,a2,b2,c2,d2;
while(~scanf("%d",&n)&&n)
{
mem(t,0);
int num=0;
for(int i=0; i<n; i++)
{
scanf("%d%d%d%d%d%d%d%d",&a1,&b1,&c1,&d1,&a2,&b2,&c2,&d2);
X[num]=a1;
e[num++]=Seg(a1,c1,b1,1);
X[num]=c1;
e[num++]=Seg(a1,c1,b2,-1);
X[num]=a1;
e[num++]=Seg(a1,c1,d2,1);
X[num]=c1;
e[num++]=Seg(a1,c1,d1,-1);
X[num]=a1;
e[num++]=Seg(a1,a2,b2,1);
X[num]=a2;
e[num++]=Seg(a1,a2,d2,-1);
X[num]=c2;
e[num++]=Seg(c2,c1,b2,1);
X[num]=c1;
e[num++]=Seg(c2,c1,d2,-1);
}
sort(X,X+num);
sort(e,e+num);
int m=unique(X,X+num)-X;
ll ans=0;
for(int i=0; i<num; i++)
{
int l=lower_bound(X,X+m,e[i].l)-X;
int r=lower_bound(X,X+m,e[i].r)-X-1;
update(l,r,0,m,1,e[i].f);
ans+=(ll)t[1].len*(e[i+1].h-e[i].h);
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: