HDU 1542 Atlantis(水)

题目大意

%   给定若干个可重叠的矩阵,求矩阵总面积。
  数据范围 矩阵的个数不超过一百,矩阵顶点坐标不超过十的五次方。

题解

离散化,扫描线,统计答案时用线段树处理被覆盖的总长度。(其实就是裸题)
值得一说的是代码。本代码运用的线段树用了:指针,结构体,动态申请空间,真·维护区间(26行)。(其实就是为了存份扫描线的线段树代码……

#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
struct line{
	double l,r,h;
	int f;
	line(double tl=0,double tr=0,double th=0,int tf=0):l(tl),r(tr),h(th),f(tf){}
	bool operator<(const line &x)const {return h<x.h;}
}le[maxn<<1];
double pos[maxn<<1];
struct tree{
	int l,r,cnt;
	double len;
	tree *left,*right;
	tree(int tl=0,int tr=0):l(tl),r(tr),len(0),cnt(0),left(NULL),right(NULL){
		if(tl+1==tr) return;
		int mid=(tl+tr)>>1;
		left=new tree(tl,mid);
		right=new tree(mid,tr);
	}
	void calc(){
		if(cnt) len=pos[r]-pos[l];
		else len=(left?left->len:0)+(right?right->len:0);
	}
	void update(int tl,int tr,int val){
		if(l==tl&&r==tr){
			cnt+=val;
			calc();
			return;
		}
		if(left&&tr<=left->r) left->update(tl,tr,val);
		else if(right&&tl>=right->l) right->update(tl,tr,val);
		else if(left) left->update(tl,left->r,val),right->update(right->l,tr,val);
		calc();
	}
};
int n,Case=0;
int did(){
	if(scanf("%d",&n)==EOF||n==0) return -1;
	int cnt=0;
	for(int i=1;i<=n;i++){
		double x1,y1,x2,y2;
		scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
		le[++cnt]=line(x1,x2,y1,1);pos[cnt]=x1;
		le[++cnt]=line(x1,x2,y2,-1);pos[cnt]=x2;
	}
	sort(pos+1,pos+1+cnt);
	sort(le+1,le+1+cnt);
	int m=unique(pos+1,pos+1+cnt)-pos-1;
	pos[m+1]=pos[m];
	tree t(1,m);
	double ans=0;
	for(int i=1;i<=cnt;i++){
		int l=lower_bound(pos+1,pos+1+m,le[i].l)-pos;
		int r=lower_bound(pos+1,pos+1+m,le[i].r)-pos;
		t.update(l,r,le[i].f);
		ans+=(le[i+1].h-le[i].h)*t.len;
	} printf("Test case #%d\n",++Case);
	printf("Total explored area: %.2f\n\n",ans);
	return 0;
}
int main(){
	while(did()==0);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值