poj1151 Atlantis(线段树+离散化+扫描线)

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17749 Accepted: 6756

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
/*
题目:给定几组数据的坐标,求所覆盖的面积
解法:开两个结构体,一个用来建立线段树,另一个储存横坐标与对应的两个纵坐标,另一个yNode数组是数据边太大,用来离散化用的,关键点是扫面线,只是每次按x的顺序依次往过走就可以,用isLeft来记录左右边,遇到右边的话计算之前判断covers值,没计算的面积,就加入
心得:看的正刚的代码,变量名都没边。搞了几天,每天回来敲一敲,还是自己敲下来了。加油!!!
Time:2014-10-1 0:30
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 202
#define lson l,mid,rt<<1
#define rson mid,r,rt<<1|1
struct Node{
	double y1,y2,len;
	int covers;
}T[MAX<<2]; 
struct Node2{
	double y1,y2,x;
	int isLeft;//标记左右边 
}xNode[MAX];
double yNode[MAX];//用来离散化 
bool cmp(Node2 a,Node2 b){
	return a.x<b.x;
}
void pushUp(int l,int r,int rt){
	if(T[rt].covers>0)
		T[rt].len=(T[rt].y2-T[rt].y1);
	else if(r-l==1) T[rt].len=0;
	else{
		T[rt].len=T[rt<<1].len+T[rt<<1|1].len;
	}
}
void build(int l,int r,int rt){
	T[rt].y1=yNode[l];
	T[rt].y2=yNode[r];
	T[rt].covers=0;
	T[rt].len=0;
	if(r-l==1)return;
	int mid=(l+r)>>1;
	build(lson);
	build(rson);
}
void update(Node2 x,int l,int r,int rt){
	if(x.y1==T[rt].y1&&x.y2==T[rt].y2){
		T[rt].covers+=x.isLeft;
		pushUp(l,r,rt);
		return;
	}
	int mid=(l+r)>>1;
	if(x.y2<=yNode[mid])update(x,lson);
	else if(x.y1>=yNode[mid])update(x,rson);
	else{
		Node2 x1,x2;
		x1=x2=x;
		x1.y2=x2.y1=yNode[mid];
		update(x1,lson);
		update(x2,rson);
	}
	pushUp(l,r,rt);
}
void solve(){
	int N,i,id,nCase=1;
	double x1,y1,x2,y2,sum;
	while(scanf("%d",&N),N){
		for(id=0,i=0;i<N;i++){
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 
			yNode[id]=y1;
			xNode[id].x=x1;
			xNode[id].y1=y1;
			xNode[id].y2=y2;
			xNode[id++].isLeft=1;
			
			yNode[id]=y2;
			xNode[id].x=x2;
			xNode[id].y1=y1;
			xNode[id].y2=y2;
			xNode[id++].isLeft=-1;
		}
		sort(yNode,yNode+id);
		build(0,id-1,1);
		sort(xNode,xNode+id,cmp);
		update(xNode[0],0,id-1,1);
		//第一个可能没用,但可能出现前两条边是同一个矩形的边 
		for(sum=0,i=1;i<id;i++){
			sum+=T[1].len*(xNode[i].x-xNode[i-1].x);
			update(xNode[i],0,id-1,1);
		} 
		printf("Test case #%d\nTotal explored area: %.2lf\n\n",nCase++,sum);
	}
}
int main(){
	solve();
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值