HDU 1542 Atlantis
Problem Description
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.
题目大意:给出N个矩形,求所有矩形覆盖到的面积
解题报告:
扫描线SBT,我们按y离散化一边,我们把矩形拆成两条线段,进和出,对应线段树中的加减,然后按找x从小大到顺序枚举,每次贡献的答案就是:\((a[i].x-a[i-1].x)*len[1]\)其中\(len[1]\)为处于\(a[i-1].x\)到\(a[i].x\)之间的线段长度总和,len用线段树维护即可
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define ls (node<<1)
#define rs (node<<1|1)
using namespace std;
const int N=2005;
int n,m=0,num=0;double b[N];
struct node{
double l,r,x;int d;
node(){}
node(double _l,double _r,int _d,double _x){
l=_l;r=_r;d=_d;x=_x;
}
bool operator <(const node &pp)const{
return x<pp.x;
}
}a[N];
int Tree[N<<2];double len[N<<2];
void upd(int node,int l,int r){
if(Tree[node])len[node]=b[r+1]-b[l];
else if(l==r)len[node]=0;
else len[node]=len[ls]+len[rs];
}
void updata(int l,int r,int node,int sa,int se,int to){
if(l>se || r<sa)return ;
if(sa<=l && r<=se){
Tree[node]+=to;upd(node,l,r);
return ;
}
int mid=(l+r)>>1;
updata(l,mid,ls,sa,se,to);updata(mid+1,r,rs,sa,se,to);
upd(node,l,r);
}
void Clear(){
m=num=0;int tmp=N<<2;
for(RG int i=0;i<tmp;i++)Tree[i]=len[i]=0;
}
void work(int kase)
{
Clear();
double x,y,x2,y2;
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
a[++m]=node(y,y2,1,x);
a[++m]=node(y,y2,-1,x2);
b[++num]=y;b[++num]=y2;
}
sort(a+1,a+m+1);sort(b+1,b+num+1);
int tot=unique(b+1,b+num+1)-b-1;
for(int i=1;i<=m;i++){
a[i].l=lower_bound(b+1,b+tot+1,a[i].l)-b;
a[i].r=lower_bound(b+1,b+tot+1,a[i].r)-b;
}
double ans=0;tot--;
for(int i=1;i<=m;i++){
if(i>1)ans+=(a[i].x-a[i-1].x)*len[1];
updata(1,tot,1,a[i].l,a[i].r-1,a[i].d);
}
printf("Test case #%d\n",kase);
printf("Total explored area: %.2lf\n\n",ans);
}
int main()
{
int kase=0;
while(~scanf("%d",&n)){
if(!n)break;
work(++kase);
}
return 0;
}