您的位置:首页 > 理论基础 > 数据结构算法

POJ1151 亚特兰蒂斯

2016-02-25 10:22 447 查看
Atlantis

Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u
Submit Status

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

还是裸的扫描线矩形面积并,不过输入变成了double型,离散一下就好了。

#include<cstdio>
#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=100+5;
const int k=1;
int n;
struct line
{
double x,y1,y2;
int flag;
line(double x,double y1,double y2,int flag)
{
this->x=x;
this->y1=y1;
this->y2=y2;
this->flag=flag;
}
bool operator<(const line&p)const
{
return x<p.x;
}
};
vector<line>L;
int tot;
double det[2*maxn];
int find(double x)
{
return lower_bound(det+1,det+tot+1,x)-det;
}
struct node
{
int l,r,flag;
double len[5];
void set(int l,int r,int flag)
{
this->l=l;
this->r=r;
this->flag=flag;
}
}T[8*maxn];
void pushup(int i)
{
int l=T[i].l;
int r=T[i].r;
for(int j=0;j<=k;j++)T[i].len[j]=0;
if(l==r)
{
int t=min(T[i].flag,k);
T[i].len[t]=det[l+1]-det[l];
}
else
{
for(int j=0;j<=k;j++)
{
int t=min(T[i].flag+j,k);
T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
}
}
}
void build(int i,int l,int r)
{
T[i].set(l,r,0);
if(l==r)
{
pushup(i);
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
pushup(i);
}
void updata(int i,int L,int R,int x)
{
int l=T[i].l;
int r=T[i].r;
if(l>=L&&r<=R)
{
T[i].flag+=x;
pushup(i);
return ;
}
int mid=(l+r)>>1;
if(L<=mid)updata(i<<1,L,R,x);
if(R>mid)updata(i<<1|1,L,R,x);
pushup(i);
}
int main()
{
int cas=0;
double x1,y1,x2,y2;
while(scanf("%d",&n)!=EOF&&n)
{
cas++;
tot=0;
L.clear();
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
det[++tot]=y1;
det[++tot]=y2;
L.push_back(line(x1,y1,y2,1));
L.push_back(line(x2,y1,y2,-1));
}
sort(det+1,det+tot+1);
int t=tot;
tot=unique(det+1,det+t+1)-det-1;
sort(L.begin(),L.end());
build(1,1,tot);

double ans=0;
for(int i=0;i<L.size()-1;i++)
{
int l=find(L[i].y1);
int r=find(L[i].y2);
updata(1,l,r-1,L[i].flag);
ans+=T[1].len[k]*(L[i+1].x-L[i].x);
}
printf("Test case #%d\n",cas);
printf("Total explored area: %.2lf\n",ans);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  扫描线 数据结构