跳到正文

个人记录

0x43 线段树 例题4:亚特兰蒂斯

2018chenyu约 6 分钟
原文
文章信息与版本

文章信息

发布
2019年11月23日
收录
2026年8月15日
更新
2026年11月7日
篇幅
约 6 分钟

版本

  1. v1当前
代码
【题意】
有几个古希腊书籍中包含了对传说中的亚特兰蒂斯岛的描述。
其中一些甚至包括岛屿部分地图。
但不幸的是,这些地图描述了亚特兰蒂斯的不同区域。
您的朋友Bill必须知道地图的总面积。
你自告奋勇写了一个计算这个总面积的程序。 
 
【输入格式】
输入包含多组测试用例。
对于每组测试样例,第一行包含整数n,表示总的地图数量。
接下来n行,描绘了每张地图,每行包含四个数字x1,y1,x2,y2(不一定是整数),(x1,y1)和(x2,y2)分别是地图的左下角位置和右上角位置。
当输入样例n=0时,表示输入终止,该样例无需处理。 
【输出格式】
每组测试用例输出两行。
第一行输出”Test case #k”,其中k是测试样例的编号,从1开始。
第二行输出“Total explored area:a”,其中a是总地图面积,即此测试样例中所有矩形的面积并,
注意如果一片区域被多个地图包含,则在计算总面积时只计算一次,精确到小数点后两位数。
在每个测试用例后输出一个空行。 
【数据范围】

1≤n≤1001≤n≤100
0≤x1<x2≤1000000≤x1<x2≤100000
0≤y1<y2≤1000000≤y1<y2≤100000

代码
【输入样例】
2
10 10 20 20
15 15 25 25.5
0 
【输出样例】
Test case #1
Total explored area: 180.00  

扫描线直接强上就好了,由于多组数据——记得trlen=0!!然后可以小小大大地优化一下常数——加一个double类型的读入优化跑的飞快

C++
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 105
using namespace std;
typedef double db;
inline db read()
{
    db x=0.0,y=0.1;char ch=getchar();
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10.0+1.0*(ch^48),ch=getchar();
    if(ch=='.')
    {
        ch=getchar();
        do
		{
            x+=y*(ch^48);
            y/=10;
            ch=getchar();
        }while(ch>='0'&&ch<='9');
    }
    return x;
}
int cnt;db Y[N<<1],ys[N<<1];
struct edge{db x,h,l;int flag;}e[N<<1];
inline bool cmp(edge a,edge b){return a.x<b.x;}
inline int findpos(int l,int r,db val)
{
	int mid;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(ys[mid]<val) l=mid+1;
		else if(ys[mid]>val) r=mid-1;
		else break;
	}
	return mid;
}
struct segment{int l,r,lc,rc,cnt;db sum;}tr[N<<2];int trlen;
//cnt表示覆盖[l,r]整段的入边数-出边数,sum表示本[l,r]内被覆盖的长度 
void bt(int l,int r)
{
	int now=++trlen;
	tr[now].l=l;tr[now].r=r;
	if(l==r) tr[now].lc=tr[now].rc=-1;
	else
	{
		int mid=(l+r)>>1;
		tr[now].lc=trlen+1;bt(l,mid);
		tr[now].rc=trlen+1;bt(mid+1,r);
	}
	tr[now].cnt=0;tr[now].sum=0.0;
}
void pushUp(int now)
{
	int lc=tr[now].lc,rc=tr[now].rc;
    if(tr[now].cnt) tr[now].sum=ys[tr[now].r+1]-ys[tr[now].l];//整段覆盖
	else if(tr[now].l==tr[now].r) tr[now].sum=0.0;//叶子
    else tr[now].sum=tr[lc].sum+tr[rc].sum;//部分覆盖
}
void update(int now,int l,int r,int val)
{
    if(l<=tr[now].l && tr[now].r<=r)
    {
        tr[now].cnt+=val;
        pushUp(now);
        return ;
    }
    int mid=(tr[now].l+tr[now].r)>>1;
    int lc=tr[now].lc,rc=tr[now].rc;
    if(l<=mid) update(lc,l,r,val);
    if(r>mid) update(rc,l,r,val);
    pushUp(now);//计算该区间被覆盖的总长度
}
int main()
{
	int n,T=0;
	db x1,x2,y1,y2;
	while(~scanf("%d",&n))
	{
		if(!n) break;
		for(int i=1;i<=n;i++)
		{
			x1=read();y1=read();x2=read();y2=read();
			e[i*2-1].l=e[i*2].l=y1;
			e[i*2-1].h=e[i*2].h=y2;
			e[i*2-1].x=x1;e[i*2].x=x2;
			e[i*2-1].flag=1;e[i*2].flag=-1;
			Y[i]=y1;Y[i+n]=y2;
		}
		sort(Y+1,Y+n*2+1);ys[cnt=1]=Y[1];
		for(int i=2;i<=n*2;i++) if(Y[i]>Y[i-1]) cnt++,ys[cnt]=Y[i];
		trlen=0;
		bt(1,cnt);
		sort(e+1,e+n*2+1,cmp);
		db ans=0;
		for(int i=1;i<=n*2;i++)
		{
			int l=findpos(1,cnt,e[i].l),r=findpos(1,cnt,e[i].h)-1;
			update(1,l,r,e[i].flag);
	        ans+=tr[1].sum*(e[i+1].x-e[i].x);//求面积
		}
		T++;printf("Test case #%d\nTotal explored area: %.2lf\n\n",T,ans);
	}
	return 0;
}
原文