您的位置:首页 > 其它

HDU_1542 Atlantis[线段树扫描线]

2014-12-16 23:26 120 查看
传送门:HDU_1542

Atlantis

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

2
10 10 20 20
15 15 25 25.5
0

 

[align=left]Sample Output[/align]

Test case #1
Total explored area: 180.00

题意:求矩形并。

思路:线段树扫描线。

线段树维护左右区间大小,矩形下边标记为1,上边标记为-1,从下往上扫描。

参考:lv

代码:

/************************************************
* Author: Ac_sorry
* File:
* Create Date:
* Motto: One heart One life
* CSDN: http://blog.csdn.net/code_or_code *************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<string>
#include<map>
#include<utility>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define seed_ 131
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof a)
#define flag(i) tree[i].flag
#define ls(i) tree[i].ls
#define rs(i) tree[i].rs
using namespace std;
typedef long long LL;

const int N=220;

struct Edge{
double lx,rx,y;
int flag;
void init(double a,double b,double c,int d)
{
lx=a;rx=b;y=c;flag=d;
}
bool operator <(const Edge e) const
{
return y<e.y;
}
}edge[N<<2];

struct Tree{
int ls,rs;
double len;
int flag;
}tree[N<<2];

double X[N<<2];

void pushup(int p)
{
if(flag(p))
tree[p].len=X[rs(p)]-X[ls(p)];
else if(rs(p)==ls(p)+1)
tree[p].len=0;
else
tree[p].len=tree[p<<1].len+tree[p<<1|1].len;
}

void build(int p,int l,int r)
{
tree[p].len=0;
flag(p)=0;
ls(p)=l;
rs(p)=r;
if(l+1==r) return;

int m=(l+r)>>1;
build(p<<1,l,m);
build(p<<1|1,m,r);

}

void update(int p,int l,int r,int v)
{
//cout<<"---\n";
if(ls(p)>r||rs(p)<l) return;
if(l<=ls(p)&&rs(p)<=r)
{
flag(p)+=v;
pushup(p);
return;
}
//int m=(ls(p)+rs(p))>>1;
//if(l<=m)
update(p<<1,l,r,v);
//if(r>m)
update(p<<1|1,l,r,v);
pushup(p);
}

int main()
{
//int T;scanf("%d",&T);
int AC=0;
int n;
while(scanf("%d",&n)==1,n)
{
double x1,y1,x2,y2;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
edge[i].init(x1,x2,y1,-1);
edge[n+i].init(x1,x2,y2,1);
X[i]=x1;X[i+n]=x2;
}
n<<=1;
sort(edge+1,edge+1+n);
sort(X+1,X+1+n);
int cnt=unique(X+1,X+1+n)-(X+1);
build(1,1,cnt);
double ans=0;
printf("Test case #%d\n",++AC);
for(int i=1;i<=n;i++)
{

if(i!=1)
ans+=(edge[i].y-edge[i-1].y)*tree[1].len;
int mp_l,mp_r;
mp_l=lower_bound(X+1,X+1+cnt,edge[i].lx)-X;
mp_r=lower_bound(X+1,X+1+cnt,edge[i].rx)-X;
//cout<<cnt<<mp_l<<"--"<<mp_r<<endl;
update(1,mp_l,mp_r,edge[i].flag);
}
printf("Total explored area: %.2f\n\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: