hdu 3265 线段树求矩形面积并
Posters
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2061 Accepted Submission(s): 455
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.
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.
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
Recommend
lcy
一个矩形分成四个矩形来做,水题。。
View Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 222222;
double sum[maxn<<2];
int cover[maxn<<2];
double x[maxn];
struct seg{
double l,r,h;
int flag;
seg(){}
seg(double _l,double _r,double _h,int _flag):l(_l),r(_r),h(_h),flag(_flag){}
bool operator <(const seg &cmp) const {
return h<cmp.h;
}
}horizontal_seg[maxn<<1];
void pushup(int rt,int l,int r){
if(cover[rt]){
sum[rt]=x[r+1]-x[l];
}
else if(l==r){
sum[rt]=0;
}
else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&r<=R){
cover[rt]+=c;
pushup(rt,l,r);
return ;
}
int m=(l+r)>>1;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
pushup(rt,l,r);
}
int bin(double key,int n,double x[]){
int l=0,r=n-1,mid,best=-1;
while(l<=r){
mid=(l+r)>>1;
if(x[mid]<=key){
best=mid;
l=mid+1;
}
else r=mid-1;
}
return best;
}
int main(){
int n,i,k,tot1,tot2;
double x1,x2,y1,y2,x3,x4,y3,y4;
while(scanf("%d",&n),n){
tot1=0;tot2=0;
for(i=1;i<=n;i++){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
x[tot2++]=x1;
if(x3!=x1){
x[tot2++]=x3;
horizontal_seg[tot1++]=seg(x1,x3,y1,1);
horizontal_seg[tot1++]=seg(x1,x3,y2,-1);
}
x[tot2++]=x4;
if(x2!=x4){
x[tot2++]=x2;
horizontal_seg[tot1++]=seg(x4,x2,y1,1);
horizontal_seg[tot1++]=seg(x4,x2,y2,-1);
}
horizontal_seg[tot1++]=seg(x3,x4,y1,1);
horizontal_seg[tot1++]=seg(x3,x4,y3,-1);
horizontal_seg[tot1++]=seg(x3,x4,y4,1);
horizontal_seg[tot1++]=seg(x3,x4,y2,-1);
}
sort(x,x+tot2);
sort(horizontal_seg,horizontal_seg+tot1);
for(i=1,k=1;i<tot2;i++){
if(x[i]!=x[i-1]) x[k++]=x[i];
}
memset(cover,0,sizeof(cover));
memset(sum,0,sizeof(sum));
double area=0;
for(i=0;i<tot1-1;i++){
int left=bin(horizontal_seg[i].l,k,x);
int right=bin(horizontal_seg[i].r,k,x)-1;
update(left,right,horizontal_seg[i].flag,0,k-1,1);
area+=sum[1]*(horizontal_seg[i+1].h-horizontal_seg[i].h);
}
printf("%.0lf\n",area);
}
return 0;
}