Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20132 Accepted: 7602
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 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
Source
Mid-Central European Regional Contest 2000
Sol:
先离散化,按x轴的坐标排序,统计每一段x的y坐标长度.
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 105
#define eps 1e-6
#define lc (o<<1)
#define rc (o<<1|1)
#define mm ((l+r)>>1)
int n,cs,t,cnt;double area;double y[N<<1];
struct line{double x,y1,y2;int c;
void in(double a,double p,double q,int b){c=b,x=a,y1=p,y2=q;}}ll[N<<1];
int cmp(line a,line b){return a.x<b.x;}
struct Seg{
struct Node{double ql,qr,len;int s;
void Updata(int x){s+=x;if(!s)len=0;else len=qr-ql;}}d[N<<3];
void Build(int o,int l,int r){
d[o].ql=y[l],d[o].qr=y[r];d[o].len=d[o].s=0;
if(l+1>=r) return;
Build(lc,l,mm);Build(rc,mm,r);
}
void Updata(int o,int l,int r,line p){
if(l+1==r){d[o].Updata(p.c);return;}
if(p.y1<d[lc].qr) Updata(lc,l,mm,p);
if(p.y2>d[rc].ql) Updata(rc,mm,r,p);
d[o].len=d[lc].len+d[rc].len;
}
}seg;
int main(){
double x1,y1,x2,y2;
while(scanf("%d",&n),area=0,t=0,n){
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
ll[t].in(x1,y1,y2,1);y[t++]=y1;
ll[t].in(x2,y1,y2,-1);y[t++]=y2;
}
sort(y,y+t);
cnt=unique(y,y+t)-y;
sort(ll,ll+t,cmp);
seg.Build(1,0,cnt);seg.Updata(1,0,cnt,ll[0]);
for(int i=1;i<t;i++){
area+=seg.d[1].len*(ll[i].x-ll[i-1].x);
seg.Updata(1,0,cnt,ll[i]);
}
printf("Test case #%d\n",++cs);
printf("Total explored area: %.2f\n\n",area);
}
return 0;
}