poj1151平面矩形面积并,双离散化

线段树维护扫描线,表示这里的线段树维护多个矩形覆盖同一段区间时不好搞

这里的线段树需要灵活一点

扫描线记录当前扫描所在位置,扫描了多少区域(线段树维护),乘上距离,相加得答案即可

线段树维护,加一条边,在区间内加

删除一条边,区间内删除,tree【i】.s记录区间内被多少线段覆盖

如果没有被线段覆盖,则通过左右子树被线段覆盖的长度相加得到

struct seg//扫描线线段树的一般框架,关键是有一个被覆盖次数,以及特有计算长度的函数
{
         int a,b;
         int len;
         int s; //被覆盖的次数
};

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;


int n,kase=0;
struct aa
{
	double x,l,r;int t;
	bool operator <(const aa &b)const
	{
		return x<b.x;
	}
}a[2009];
struct aaa
{
	int l,r,s;//s表示覆盖情况,覆盖了几层
	double len;//表示真实长度
}tree[1009];//线段树
double hs[209];//y坐标的hash
void build(int i,int l,int r)
{
	tree[i].l=l;tree[i].r=r;
	tree[i].len=tree[i].s=0;
	if (l+1==r) return ;
	int mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid,r);
}
void pan(int i)//计算长度,如果被覆盖,则为总长,否则为左右儿子长度之和
{
	if (tree[i].s) tree[i].len=hs[tree[i].r]-hs[tree[i].l];//在线段树中,有的区间是被多个矩形覆盖 
	else 												   //这里的s,表示整个区间被覆盖了几次 
	if (tree[i].l+1==tree[i].r) tree[i].len=0;			   //因为保证删除后和插入之前一样,所有没有 
	else tree[i].len=tree[i<<1].len+tree[i<<1|1].len;	   //标记下传,QAQ感觉思路有点乱 
}
void updata(int i,int l,int r,int t)//插入或删除一条线段
{
	if (tree[i].l==l&&tree[i].r==r)
	{
		tree[i].s+=t;
		pan(i);
		return ;
	}
	int mid=(tree[i].l+tree[i].r)>>1;
	if (mid>=r) updata(i<<1,l,r,t);
	else if (mid<=l) updata(i<<1|1,l,r,t);
	else updata(i<<1,l,mid,t),updata(i<<1|1,mid,r,t);
	pan(i);
}
void work()
{
	double aa,bb,cc,dd;
	int tot=0;
	for (int i=1;i<=n;i++)
	{
		scanf("%lf%lf%lf%lf",&aa,&bb,&cc,&dd);
		a[++tot].x=aa;a[tot].l=bb;a[tot].r=dd;a[tot].t=1;hs[tot]=bb;
		a[++tot].x=cc;a[tot].l=bb;a[tot].r=dd;a[tot].t=-1;hs[tot]=dd;
	}
	sort(hs+1,hs+tot+1);
	sort(a+1,a+tot+1);
	int nn=unique(hs+1,hs+tot+1)-hs-1;
	build(1,1,nn);
	int l,r;
	double ans=0;
	for (int i=1;i<=tot;i++)
	{
		l=lower_bound(hs+1,hs+nn+1,a[i].l)-hs;
		r=lower_bound(hs+1,hs+nn+1,a[i].r)-hs;
		ans+=(a[i].x-a[i-1].x)*tree[1].len;
		updata(1,l,r,a[i].t);
	}
	printf("Test case #%d\n",++kase);
	printf("Total explored area: %.2lf\n\n",ans);
}
int main()
{
	while (scanf("%d",&n)!=EOF)
	{
		if (n==0) break;
		sizeof(a,0,sizeof(a));
		sizeof(hs,0,sizeof(hs));
		sizeof(tree,0,sizeof(tree));
		work();
	}
	return 0;
}


暴力:

离散化后,数据范围小,直接枚举,标记所有可行点就行

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;

double x[109],y[109],xx[109],yy[109];

int n,kase=0;
double hsy[209],hsx[209];
bool f[209][209];
void work()
{
	int tot=0;
	for (int i=1;i<=n;i++) 
	{
		scanf("%lf%lf%lf%lf",&x[i],&y[i],&xx[i],&yy[i]);
		hsx[++tot]=x[i];hsy[tot]=y[i];
		hsx[++tot]=xx[i];hsy[tot]=yy[i];
	}
	sort(hsx+1,hsx+tot+1);
	sort(hsy+1,hsy+tot+1);
	int nx=unique(hsx+1,hsx+tot+1)-hsx-1;
	int ny=unique(hsy+1,hsy+tot+1)-hsy-1;
	double ans=0;
	int a,b,c,d;
	for (int i=1;i<=n;i++)
	{
		a=lower_bound(hsx+1,hsx+nx+1,x[i])-hsx;
		b=lower_bound(hsx+1,hsx+nx+1,xx[i])-hsx;
		c=lower_bound(hsy+1,hsy+ny+1,y[i])-hsy;
		d=lower_bound(hsy+1,hsy+ny+1,yy[i])-hsy;
		for (int i=a;i<b;i++)
			for (int j=c;j<d;j++) f[i][j]=true;
	}
	for (int i=1;i<nx;i++)
		for (int j=1;j<ny;j++)
		if (f[i][j]) ans+=(hsy[j+1]-hsy[j])*(hsx[i+1]-hsx[i]);
	printf("Test case #%d\n",++kase);
	printf("Total explored area: %.2lf\n\n",ans);
	return ;
}
int main()
{
	while (scanf("%d",&n)!=EOF)
	{
		if (n==0) break;
		memset(hsx,0,sizeof(hsx));
		memset(hsy,0,sizeof(hsy));
		memset(f,false,sizeof(f));
		work();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值