Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9662 Accepted Submission(s): 4129
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
恩,题目大意就是说,求n个矩形的面积并,对于每个矩形,给出左下角坐标和右上角坐标,套的模板(学习的康神)poj上提交时最后输出是 “%.2f"不是 ”%.2lf"
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2200
using namespace std;
double rec[maxn],sum[maxn];
int tree[maxn];
struct node
{
double x1,x2,y;
int s;
}a[maxn];
bool cmp(node a,node b)
{
return a.y<b.y;
}
int bsearch(int l,int r,double aim)
{
while(l<=r)
{
int mid=(l+r)>>1;
if(rec[mid]==aim)
return mid;
else if(rec[mid]>aim)
r=mid-1;
else
l=mid+1;
}
}
void pushup(int o,int l,int r)
{
if(tree[o])
sum[o]=rec[r+1]-rec[l];
else if(l==r)
sum[o]=0;
else
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void update(int l,int r,int L,int R,int o,int sign)
{
if(l<=L&&r>=R)
{
tree[o]+=sign;
pushup(o,L,R);
return ;
}
int mid=(L+R)>>1;
if(r<=mid)
update(l,r,L,mid,o<<1,sign);
else if(l>mid)
update(l,r,mid+1,R,o<<1|1,sign);
else
{
update(l,mid,L,mid,o<<1,sign);
update(mid+1,r,mid+1,R,o<<1|1,sign);
}
pushup(o,L,R);
}
int main()
{
int n,cnt=0;
double x1,y1,x2,y2;
while(scanf("%d",&n)&&n)
{
memset(rec,0,sizeof(rec));
memset(tree,0,sizeof(tree));
memset(sum,0,sizeof(sum));
int k=1;
for(int i=0;i<n;++i)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
a[k].x1=x1;
a[k].x2=x2;
a[k].y=y1;
a[k].s=1;
rec[k++]=x1;
a[k].x1=x1;
a[k].x2=x2;
a[k].y=y2;
a[k].s=-1;
rec[k++]=x2;
}
sort(rec+1,rec+k);
sort(a+1,a+k,cmp);
int j=2;
for(int i=2;i<k;++i)
{
if(rec[i]!=rec[i-1])
rec[j++]=rec[i];
}
double ans=0;
for(int i=1;i<k-1;++i)
{
int l=bsearch(1,j,a[i].x1);
int r=bsearch(1,j,a[i].x2)-1;
if(l<=r)
update(l,r,1,k-1,1,a[i].s);
ans+=sum[1]*(a[i+1].y-a[i].y);
}
printf("Test case #%d\n",++cnt);
printf("Total explored area: %.2lf\n\n",ans);
}
return 0;
}