hdu1542 Atlantis 线段树--扫描线求面积并

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

题意:给出若干个矩形,求他们的总面积,即矩形的面积并

求面积并是线段树-扫描线的裸题

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxm=; int cov[maxm<<];
double y[maxm],st[maxm]; struct seg{
double x,y1,y2;
int c;
bool operator < (const seg a)const{
return x<a.x;
}
}s[maxm]; void pushup(int o,int l,int r){
if(cov[o]>)st[o]=y[r]-y[l];
else if(cov[o]==){
if(l+==r)st[o]=;
else st[o]=st[o<<]+st[o<<|];
}
} void update(int o,int l,int r,seg a){
if(a.y1<=y[l]&&a.y2>=y[r]){
cov[o]+=a.c;
pushup(o,l,r);
return;
}
if(l+==r)return;
int m=l+((r-l)>>);
if(a.y1<y[m])update(o<<,l,m,a);
if(a.y2>y[m])update(o<<|,m,r,a);
pushup(o,l,r);
} int main(){
int n;
int c=;
while(scanf("%d",&n)!=EOF&&n){
memset(st,,sizeof(st));
memset(cov,,sizeof(cov));
for(int i=;i<=n;++i){
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
s[*i-].x=x1;s[*i-].y1=y1;s[*i-].y2=y2;s[*i-].c=;
y[*i-]=y1;
s[*i].x=x2;s[*i].y1=y1;s[*i].y2=y2;s[*i].c=-;
y[*i]=y2;
}
sort(s+,s+*n+);
sort(y+,y+*n+);
double ans=;
int cnt=;
for(int i=;i<=*n;++i){
if(y[i]!=y[i-])y[++cnt]=y[i];
}
for(int i=;i<*n;++i){
update(,,cnt,s[i]);
ans+=st[]*(s[i+].x-s[i].x);
}
printf("Test case #%d\n",++c);
printf("Total explored area: %.2lf\n\n",ans);
}
return ;
}
上一篇:Jquery Mobile转场特效之slide | 小小iPhone开发


下一篇:【CodeForces 624C】Graph and String