Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16244 | Accepted: 6183 |
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.
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 <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define N 100000
using namespace std;
struct num
{
double x1,y1,x2,y2;
}a[N],b[N];
int Top,pos;
bool uv;
bool cmp(const struct num &p1,const struct num &p2)
{
return p1.x1<p2.x1;
}
int main()
{
//freopen("data.txt","r",stdin);
void ch();
void add(double x1,double y1,double x2,double y2,int p);
int n;
int T=1;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
for(int i=0;i<=n-1;i++)
{
scanf("%lf %lf %lf %lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
}
sort(a,a+n,cmp);
Top = 0;
for(int i=0;i<=n-1;i++)
{
pos = Top;
double x3 = a[i].x1,y3 = a[i].y1;
double x4 = a[i].x2,y4 = a[i].y2;
bool in = false;
for(int j=0;j<=Top-1;j++)
{
double x1 = b[j].x1,y1 = b[j].y1;
double x2 = b[j].x2,y2 = b[j].y2;
if(x3>x2||x1>x4||y3>y2||y1>y4)
{
continue;
}
in = true;
pos = j;
uv = false;
double k1 = max(x1,x3);
double k2 = min(x4,x2);
if(k1>x1)
{
add(x1,y1,k1,y2,pos);
ch();
}
if(k2<x2)
{
add(k2,y1,x2,y2,pos);
ch();
}
double k3 = max(y3,y1);
double k4 = min(y4,y2);
if(k3>y1)
{
add(k1,y1,k2,y3,pos);
ch();
}
if(k4<y2)
{
add(k1,y4,k2,y2,pos);
ch();
}
}
add(x3,y3,x4,y4,pos);
if(!in)
{
Top++;
continue;
}
ch();
}
double sum = 0;
for(int i=0;i<=Top-1;i++)
{
sum+=(b[i].x2-b[i].x1)*(b[i].y2-b[i].y1);
}
printf("Test case #%d\n",T++);
printf("Total explored area: %.2lf\n",sum);
printf("\n");
}
return 0;
}
void ch()
{
if(uv)
{
Top++;
}
pos = Top;
uv = true;
}
void add(double x1,double y1,double x2,double y2,int p)
{
b[p].x1 = x1;
b[p].y1 = y1;
b[p].x2 = x2;
b[p].y2 = y2;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define N 500
using namespace std;
struct num
{
double x,y1,y2;
int deg;
}infor[N];
struct tree
{
int l,r,c;
double ly,ry,dis;
}a[4*N];
double b[N];
bool cmp(num p1,num p2)
{
return p1.x<p2.x;
}
int main()
{
void build(int k,int l,int r);
void update(int k,num p);
int n;
int T = 1;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
int t = 1;
for(int i=1;i<=n;i++)
{
double x1,y1,x2,y2;
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
infor[t].x = x1;
infor[t].y1 = y1;
infor[t].y2 = y2;
infor[t].deg = 1;
b[t++] = y1;
infor[t].x = x2;
infor[t].y1 = y1;
infor[t].y2 = y2;
infor[t].deg = -1;
b[t++] = y2;
}
sort(infor+1,infor+t,cmp);
sort(b+1,b+t);
build(1,1,t-1);
update(1,infor[1]);
double res = 0;
for(int i=2;i<=t-1;i++)
{
res+=(infor[i].x-infor[i-1].x)*a[1].dis;
update(1,infor[i]);
}
printf("Test case #%d\n",T++);
printf("Total explored area: %.2lf\n",res);
printf("\n");
}
return 0;
}
void build(int k,int l,int r)
{
a[k].l = l;
a[k].r = r;
a[k].dis = a[k].c = 0;
a[k].ly = b[l];
a[k].ry = b[r];
if(l+1==r)
{
return ;
}
int mid = (l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid,r);
}
void cal(int k)
{
if(a[k].c>0)
{
a[k].dis = a[k].ry - a[k].ly;
return ;
}
if(a[k].l+1==a[k].r)
{
a[k].dis = 0;
}else
{
a[k].dis = a[k<<1].dis+a[k<<1|1].dis;
}
}
void update(int k,num p)
{
if(a[k].ly==p.y1&&a[k].ry==p.y2)
{
a[k].c+=p.deg;
cal(k);
return ;
}
if(p.y2<=a[k<<1].ry)
{
update(k<<1,p);
}else if(p.y1>=a[k<<1|1].ly)
{
update(k<<1|1,p);
}else
{
num e = p;
e.y2 = a[k<<1].ry;
update(k<<1,e);
e = p;
e.y1 = a[k<<1|1].ly;
update(k<<1|1,e);
}
cal(k);
}