HDU 1255(线段树优化扫描线——矩形交集)

博客围绕平面上若干矩形覆盖问题展开,给出题目链接及描述,要求计算被矩形覆盖至少两次区域的面积。介绍输入输出格式,核心题意为求矩形交集面积,思路是在矩形并集基础改函数,还提及代码实现改动之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

~by Wjvje 2019-5-3

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255

题目描述:

                                  覆盖的面积

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8647    Accepted Submission(s): 4333

Problem Description

给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

Input

输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行

是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表

平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐

标的范围从0到100000。注意:本题的输入数据较多,推荐使用scanf读入数据.

Output

对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.

Sample Input

2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7

3
0 0 1 1
1 0 2 1
2 0 3 1

Sample Output

7.63
0.00

核心题意:

       矩形交集的面积。

思路分析:

       原理布吉岛,,,矩形并集改一下pushUp函数和Len2[1]即可。

代码实现:(改了两个地方Len2,和pushUp)

#include<bits/stdc++.h> 
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+100;//-----------注意4*N 
using namespace std;
struct Node
{
	double l,r,h;
	int value; 
}a[N];
bool cmp(Node aa,Node bb)
{
	return aa.h<bb.h;
}
//bool cmp2(double aa,double bb)         /这几把东西一点也不好用 
//{
//	return bb-aa>=1e-9;
//}
int Left[N],Right[N],dat[N];//左儿子节点、右儿子节点 
double Len2[N];
double xx[N],Len[N];//每个节点包含的区间中被覆盖的长度,double型 
void build(int p,int l,int r)
{
	Left[p]=l;
	Right[p]=r;//这个地方总喜欢写成2*p,2*p+1; 
	if(l==r)
	{
		dat[p]=0;
		return ;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	dat[p]=0;
}
void pushUp(int p)//-----------区间交的核心 
{
	//该区间还能被完全覆盖 
	if(dat[p]>1)
		Len[p]=Len2[p]=xx[Right[p]+1]-xx[Left[p]];
		//cout<<p<<":"<<xx[Right[p]+1]<<"==="<<xx[Left[p]]<<" "<<Len[p]<<endl;
	else if(dat[p]==1)
	{
		Len[p]=xx[Right[p]+1]-xx[Left[p]];
		if(Right[p]==Left[p])Len2[p]=0;
		else Len2[p]=Len[2*p]+Len[2*p+1];
	}
	else 
	{
		if(Right[p]==Left[p])Len[p]=Len2[p]=0;
		else
		{
			Len[p]=Len[2*p]+Len[2*p+1];
			Len2[p]=Len2[2*p]+Len2[2*p+1];
		}
		
	}
		//cout<<p<<":"<<Len[2*p]<<"+"<<Len[2*p+1]<<" "<<" "<<2*p<<endl;
	//dat==0,即没有被完全覆盖,则由子节点确定其中被覆盖的长度 
}
void change(int p,int l,int r,int v)
{ 
	if(l<=Left[p]&&r>=Right[p])
	{
		dat[p]+=v;//这个点(多个区间)的被覆盖次数,完全覆盖 ,+=v 
		pushUp(p); //被覆盖次数变化,更新自身以及祖先们的Len; 
		return ;
	}
	int mid=(Left[p]+Right[p])/2;
	if(l<=mid)change(p*2,l,r,v);
	if(r>mid)change(p*2+1,l,r,v);
	pushUp(p);//两个change导致子节点被覆盖次数变化,更新p的Len; 
	
}
int main()
{
	io;
	int n;
	int cas=0;
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int tot=0;
		int cnt=0;
		memset(Len,0,sizeof(Len));//------guan jian**
		memset(xx,0,sizeof(xx));
		memset(Left,0,sizeof(Left));
		memset(Right,0,sizeof(Right));
		memset(dat,0,sizeof(dat)); 
		
		double sum_s=0;
		for(int i=1;i<=n;++i)
		{
			double x1,y1,x2,y2;
			cin>>x1>>y1>>x2>>y2;//-----注意弄清楚哪两个角 
			//cout<<"("<<x2<<"-"<<x1<<")*("<<y2<<"-"<<y1<<")=="<<abs(x2-x1)*abs(y2-y1)<<endl;
			a[++tot].l=x1;
			a[tot].r=x2;
			a[tot].h=y1;
			a[tot].value=1;
			//cout<<"tot="<<tot<<",,,,,"<<a[tot].h<<endl;
			a[++tot].l=x1;
			a[tot].r=x2;
			a[tot].h=y2;
			a[tot].value=-1;
			//cout<<"tot="<<tot<<",,,,,, "<<a[tot].h<<endl;
			xx[++cnt]=x1;
			xx[++cnt]=x2;
		}
		sort(xx+1,xx+1+cnt);
		cnt=unique(xx+1,xx+1+cnt)-xx-1;//---------(-xx-1); 
		
//		for(int i=1;i<=cnt;++i)cout<<xx[i]<<" ? ";
//		cout<<endl;
		build(1,1,cnt-1);//--------线段树中每个叶子节点表示以该点为左边界的小区间,so:cnt-1 
		sort(a+1,a+1+tot,cmp);
		//cout<<a[10].h<<"??????????????"<<endl;
		double ans=0;
		for(int i=1;i<=tot;++i) //i==tot的时候Len【1】==0,去掉tot也可以 
		{
			//cout<<a[i].h<<"-- "<<a[i].l<<" --"<<a[i].r<<endl;
			int x=lower_bound(xx+1,xx+1+cnt,a[i].l)-xx;
			int y=lower_bound(xx+1,xx+1+cnt,a[i].r)-xx;//cout<<i<<endl;
			//cout<<x<<";;;;;;;"<<y<<" /"<<a[i].r<<endl;
			change(1,x,y-1,a[i].value);	
			ans+=(double)Len2[1]*(a[i+1].h-a[i].h);/////----------------------Len2 	
//			cout<<"length_total:   "<<Len[1]<<"   distance:    "<<
//			"("<<a[i+1].h<<"-"<<a[i].h<<") =="<<(a[i+1].h-a[i].h)<<endl;
		}
		cout<<fixed<<setprecision(2)<<ans<<endl;//cout精度控制,学习一下 
	}
	return 0;
}

 

The end;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值