Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6116 Accepted Submission(s): 2677
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.
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.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
::这道题我是参考hh博客上的代码。用线段树来保存当前被覆盖的线段长度总和。
建议:如果刚学线段树+扫描线,最好把那sample input 作为测试数据输出中间变量的值,
这样可以有更深的理解
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N=500;
double sum[N<<2];
int col[N<<2];
double x[N];
struct node
{
double l,r,h;
int s;
bool operator < (const node & t) const {
return h<t.h;
}
}e[N];
void add(node &e,double l,double r,double h,int s)
{
e.l=l; e.r=r; e.h=h; e.s=s;
}
int Bina(double x[],int n,double a)
{
int l=0,r=n-1;
while(l<=r)
{
int m=(l+r)>>1;
if(x[m]==a) return m;
if(x[m]<a) l=m+1;
else r=m-1;
}
return -1;
}
void PushUp(int rt,int l,int r)
{
if(col[rt]) sum[rt]=x[r+1]-x[l];
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)
{
col[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 main()
{
// freopen("in.txt","r",stdin);
int n,cas=1;
while(scanf("%d",&n)>0&&n)
{
int k=0,i;
for(i=0; i<n; i++)
{
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
x[k]=a;
add(e[k++],a,c,b,1);
x[k]=c;
add(e[k++],a,c,d,-1);
}
sort(e,e+k);
///////离散化////////////////////////////////////
sort(x,x+k);
int m=1;
for(i=1; i<k; i++)
{
if(x[i]!=x[i-1])
x[m++]=x[i];
}
////////////////////////////////////////////////
// for(i=0; i<m ;i++) printf("x[%d]=%lf\n",i,x[i]);
// for(i=0; i<k; i++) printf("%d-> l=%lf, r=%lf, h=%lf ..s=%d\n",i,e[i].l,e[i].r,e[i].h,e[i].s);
memset(sum,0,sizeof(sum));
memset(col,0,sizeof(col));
double ans=0;
for(i=0; i<k-1; i++)
{
int L=Bina(x,m,e[i].l);
int R=Bina(x,m,e[i].r)-1;
if(L<=R) update(L,R,e[i].s,0,k-1,1);
// printf("L=%d, x[L]=%lf ; R=%d, x[R]=%lf\n",L,x[L],R,x[R]);
// printf("覆盖区间长度=%lf\n",sum[1]);
ans+=(e[i+1].h-e[i].h)*sum[1];//e[i+1].h-e[i].h是固定的长度H,sum[1]是在覆盖在H区域内的线段有多长
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
}
return 0;
}