POJ 1151 (扫描线+线段树优化+离散化处理,模板)

~by Wjvje 2019-5-2

 

题目链接:http://poj.org/problem?id=1151

题目描述:

Atlantis

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28239 Accepted: 10135

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

核心题意:

        给出n个矩形,每个矩形给出左下角、右上角坐标(实数型),然后求这些矩形并集的面积。

思路:

       线段树优化扫描线入门题目,注意坐标是double类型。其他的再注意一下dat[i]Len[i]的含义即可,

前者表示线段树中i节点所代表的区间被完全覆盖的次数(不太准确,应该是更新时被某一个L,R完全覆

盖的次数,不包含两次L,R所产生的完全包含)。后者代表所涉及区间中当前被覆盖的长度,double类型。

实现代码:

#include<bits/stdc++.h>
#define LL long long
#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;
}
int Left[N],Right[N],dat[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])
		Len[p]=xx[Right[p]+1]-xx[Left[p]];
		//cout<<p<<":"<<xx[Right[p]+1]<<"==="<<xx[Left[p]]<<" "<<Len[p]<<endl;
	else 
		Len[p]=Len[2*p]+Len[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;
	while(cin>>n&&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)); 
		for(int i=1;i<=n;++i)
		{
			double x1,y1,x2,y2;
			cin>>x1>>y1>>x2>>y2;//-----注意弄清楚哪两个角 
			a[++tot].l=x1;
			a[tot].r=x2;
			a[tot].h=y1;
			a[tot].value=1;
			
			a[++tot].l=x1;
			a[tot].r=x2;
			a[tot].h=y2;
			a[tot].value=-1;
			
			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);
		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)Len[1]*(a[i+1].h-a[i].h);
			//cout<<",,"<<Len[1]<<"**"<<(a[i+1].h-a[i].h)<<endl;
		}
		
		cout<<"Test case #"<<++cas<<endl; 
		cout<<"Total explored area: ";
		cout<<fixed<<setprecision(2)<<ans<<endl<<endl;//cout精度控制,学习一下 
	}
	return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值