Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5956 Accepted Submission(s): 2613
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.
Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
Source
代码君:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int M=510;
using namespace std;
struct Seg{
double l,r,h;
int pos;
Seg(double l=0.0,double r=0.0,double h=0.0,int pos=1){
this->l=l; this->r=r; this->h=h; this->pos=pos;
}
bool operator<(const Seg &a)const{
return this->h<a.h;
}
};
int cnt[M<<2],n;
double X[M<<2],sum[M<<2];
Seg line[M];
void init(){
memset(sum,0,sizeof(sum));
memset(cnt,0,sizeof(cnt));
}
void PushUp(int rt,int l,int r){
if (cnt[rt]) sum[rt]=X[r+1]-X[l];
else if (l==r) sum[rt]=0.0;
else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int l,int r,int rt,int L,int R,int c){
if (L<=l && r<=R){
cnt[rt]+=c; PushUp(rt,l,r); return;
}
int m=(l+r)>>1;
if (L<=m) update(lson,L,R,c);
if (m<R) update(rson,L,R,c);
PushUp(rt,l,r);
}
int main(){
int t=0;
while (~scanf("%d",&n) && n){
init();
double a,b,c,d;
int Xcnt=0,cnt=1,Lcnt=0;
for (int i=0;i<n;++i){
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
line[Lcnt++]=Seg(a,c,b,1);
line[Lcnt++]=Seg(a,c,d,-1);
X[Xcnt++]=a; X[Xcnt++]=c;
}
sort(X,X+Xcnt); sort(line,line+Lcnt);
for (int i=1;i<Xcnt;++i)
if (X[i]!=X[i-1]) X[cnt++]=X[i];
double ret=0.0;
for (int i=0;i<Lcnt;++i){
int l,r;
l=lower_bound(X,X+cnt,line[i].l)-X; r=lower_bound(X,X+cnt,line[i].r)-X-1;
update(0,cnt-1,1,l,r,line[i].pos);
ret+=1.0*sum[1]*(line[i+1].h-line[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",++t,ret);
}
return 0;
}
思路:线段树+离散化+扫描线,需要注意的地方是扫描线的地方
r=lower_bound(X,X+cnt,line[i].r)-X-1;
和计算底边长的r+1
假设要计算的左右端点分别为left和right,则计算sum[1]时
sum[1]=X[mid]-X[left]+X[right]-X[mid+1]; 是错误的
原因是离散化之后,X[mid]~X[mid+1]这个区间是有长度的,而我们恰恰少算了这个区间
所以这里用r-1,r+1就不难理解了。