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
题意:告诉你若干矩形的两个对角点的坐标,求所有矩形的面积并。
这就是一道裸题。。先离散化横坐标,然后以横坐标上所有的边长来建树,按照纵坐标从下往上的顺序来扫,遇到下边就在树上标记加1,遇到上边就在树上标记减一。每次统计当前所有标记大于1的边的总边长,乘以当前扫过的高度,即为当前扫过的面积。
代码:
#include<map>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num;
double xx[202],d;
map<double,int> M;
struct node
{
double len;
int num;
}E[10005];
struct node2
{
double l,r,y;
bool flag;
}yy[202];
bool cmp2(node2 a,node2 b)
{
return a.y<b.y;
}
void build(int l,int r,int root)
{
E[root].num=0;
if(l==r)
{
E[root].len=xx[num+1]-xx[num];
num++;
return;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
E[root].len=E[root<<1].len+E[root<<1|1].len;
}
void update(int l,int r,int L,int R,int root,bool f)
{
if(l>=L&&r<=R)
{
if(f)
{
E[root].num++;
E[root].len=xx[r]-xx[l-1];
}
else
{
E[root].num--;
if(!E[root].num)
E[root].len=E[root<<1].len+E[root<<1|1].len;
}
return;
}
int mid=(l+r)>>1;
if(L<=mid)
update(l,mid,L,R,root<<1,f);
if(R>mid)
update(mid+1,r,L,R,root<<1|1,f);
if(!E[root].num)
E[root].len=E[root<<1].len+E[root<<1|1].len;
}
void get_ans(int l,int r,int root)
{
if(E[root].num)
{
d+=E[root].len;
return;
}
if(l==r)
return;
int mid=(l+r)>>1;
get_ans(l,mid,root<<1);
get_ans(mid+1,r,root<<1|1);
}
int main()
{
int n,t=1;
while(~scanf("%d",&n),n)
{
M.clear();
int num1=0,num2=0;
for(int i=0;i<n;i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
xx[num1++]=x1;
xx[num1++]=x2;
yy[num2].l=x1;
yy[num2].r=x2;
yy[num2].y=y1;
yy[num2++].flag=true;
yy[num2].l=x1;
yy[num2].r=x2;
yy[num2].y=y2;
yy[num2++].flag=false;
}
sort(xx,xx+num1);
for(int i=0;i<num1;i++)
M[xx[i]]=i;
sort(yy,yy+num2,cmp2);
num=0;
build(1,num1-1,1);
double ans=0;
for(int i=0;i<num2-1;i++)
{
d=0;
if(yy[i].flag)
update(1,num1-1,M[yy[i].l]+1,M[yy[i].r],1,true);
else
update(1,num1-1,M[yy[i].l]+1,M[yy[i].r],1,false);
get_ans(1,num1-1,1);
ans+=d*(yy[i+1].y-yy[i].y);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",t++,ans);
}
return 0;
}