HDU 3265 Posters
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<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=5e4+10;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
int n,m,cove[maxn<<2];
ll sum[maxn<<2];
struct Node{
int l,r,h,co;
Node(){}
Node(int l1,int r1,int h1,int s1){
l=l1;r=r1;h=h1;co=s1;
}
}seg[8*maxn];
bool cmp(Node a,Node b){
return a.h<b.h;
}
void pushup(int l,int r,int k){
if(cove[k]) sum[k]=r-l+1;
else if(l==r) sum[k]=0;
else sum[k]=sum[k<<1]+sum[k<<1|1];
}
void update(int l,int r,int ql,int qr,int val,int k){
if(ql>qr) return ;
if(ql<=l&&r<=qr){
cove[k]+=val;
pushup(l,r,k);
return ;
}
int mid=(l+r)>>1;
if(qr<=mid) update(l,mid,ql,qr,val,k<<1);
else if(ql>mid) update(mid+1,r,ql,qr,val,k<<1|1);
else update(l,mid,ql,mid,val,k<<1),update(mid+1,r,mid+1,qr,val,k<<1|1);
pushup(l,r,k);
}
int main(){
int i,j,x1,x2,x3,x4,y1,y2,y3,y4;
while(scanf("%d",&n)!=EOF){
if(!n) break;
m=0;
int lb=maxn,rb=-1;
for(i=0;i<n;i++){
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
seg[m++]=Node(x1,x2,y1,1);seg[m++]=Node(x1,x2,y3,-1);
seg[m++]=Node(x1,x2,y4,1);seg[m++]=Node(x1,x2,y2,-1);
seg[m++]=Node(x1,x3,y3,1);seg[m++]=Node(x1,x3,y4,-1);
seg[m++]=Node(x4,x2,y3,1);seg[m++]=Node(x4,x2,y4,-1);
lb=min(lb,x1);rb=max(rb,x2);
}
sort(seg,seg+m,cmp);
mem(sum,0);mem(cove,0);
ll ret=0;
for(i=0;i<m-1;i++){
update(lb,rb,seg[i].l,seg[i].r-1,seg[i].co,1);
ret+=1ll*sum[1]*(seg[i+1].h-seg[i].h);
}
printf("%lld\n",ret);
}
return 0;
}
推荐阅读
-
hdu--1232 继续通畅工程
-
HDU 2256Problem of Precision(矩阵快速幂)
-
湫湫系列故事——设计风景线 HDU - 4514
-
HDU6315 Naive Operations(线段树 复杂度分析)
-
【题解】hdu1506 Largest Rectangle in a Histogram
-
C - Monkey and Banana HDU 1069( 动态规划+叠放长方体)
-
HDU 1052(田忌赛马 贪心)
-
hdu-1338 game predictions(贪心题)
-
致初学者(四):HDU 2044~2050 递推专项习题解
-
C语言BFS--Find a way(Hdu 2612)