poj1151 Atlantis(hh版 扫描线)

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17920 Accepted: 6815

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 
/*
写这种臭长的代码很不划算,一下记不住,测试的时候还是不会,会写也是一堆错。。不过,我还是得继续臭长下去,等到写线段树之类的臭长题目到看到题目能自己写出来就说明水平真的提高了。。加油!!胡浩的模板确实挺不错的,比开结构体简单。
Time:2014-11-11 17:57
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAX=2222;
int cnt[MAX<<2];
double sum[MAX<<2];
double X[MAX]; 
struct Seg{
	double h,l,r;
	int tag;
	Seg(){}
	Seg(double a,double b,double c,int d):l(a),r(b),h(c),tag(d){};
	bool operator <(const Seg &cmp) const{
		return h<cmp.h;
	} 
}seg[MAX];
void pushUp(int l,int r,int rt){
	if(cnt[rt]){
		sum[rt]=X[r+1]-X[l];//一处错,从0 开始所以不用X[r]-X[l-1] 
	}else if(r==l){
		sum[rt]=0;
	}else{
		sum[rt]=sum[rt<<1]+sum[rt<<1|1];
	}
}
void update(int L,int R,int c,int l,int r,int rt){
	if(L<=l&&r<=R){
		cnt[rt]+=c;
		pushUp(l,r,rt);
		return;
	}
	int mid=(l+r)>>1;
	if(L<=mid)update(L,R,c,lson);
	if(R>mid)update(L,R,c,rson);
	
	pushUp(l,r,rt);
}
int bSearch(double key,int n,double *X){
	int l=0;int r=n-1;
	while(l<=r){
		int mid=(l+r)>>1;
		if(key>X[mid]) l=mid+1;
		else if(key==X[mid]) return mid;
		else if(key<X[mid]) r=mid-1;
	}
	return -1;
}
int main(){
	int n;
	int nCase=0;
	while(scanf("%d",&n)!=EOF,n){
		int k=0;
		double a,b,c,d,ans=0;
		for(int i=0;i<n;i++){
			scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
			X[k]=a;
			seg[k++]=Seg(a,c,b,1);
			
			X[k]=c;
			seg[k++]=Seg(a,c,d,-1);
		}
		sort(X,X+k);
		sort(seg,seg+k);
		
		int m=unique(X,X+k)-X;//m不能用k代替。。。。晕。。。。 
		memset(cnt,0,sizeof(cnt));
		memset(sum,0,sizeof(sum));
		for(int i=0;i<k-1;i++){
			int l=bSearch(seg[i].l,m,X);
			int r=bSearch(seg[i].r,m,X)-1;
			//printf("%d %d\n",l,r); 
			if(r>=l) update(l,r,seg[i].tag,0,m-1,1);
			ans+=sum[1]*(seg[i+1].h-seg[i].h);
		}
		printf("Test case #%d\nTotal explored area: %.2lf\n\n",++nCase,ans);
	}
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值