HDU 1542 扫描线

博客围绕Atlantis问题展开,即在二维平面放置矩形,求覆盖面积。采用扫描线思路,将扫描线存数组排序并离散化,从下往上扫描。扫描时把线信息存好,利用线段树更新区间长度计算面积,同时强调扫描线线段树需改造以处理区间问题,并给出思路。

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16397    Accepted Submission(s): 6691


Problem 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 file 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
 


题意:在一个二位平面上放置矩形,给出矩形的左下角坐标和右上角坐标,求整个二位平面的覆盖面积

思路:扫描线

先看一张我手画的样例草图


看上面的图,我做的扫面线是横线(与X轴平行),从下往上扫描

存进一个数组里从小到大排序,用下标做离散化

所以依次扫描的线是 1 线 -> 2线 -> 3线 -> 4线

每条线存了 两个x值(线段的左右端点),一个y值(线段高度),还有一个flag标记(1为矩形的下线,-1为上线)

扫描第一条线的时候,在下标数组中找到这条线的左右端点值所在的下标,然后update进线段树中

第一条线插入完毕后 tree[1].sum = 10  (sum表示整个区间的有效线段长度)

所以面积 +=  2线与1线的高度差 * tree[1].sum = 5 * 10 = 50

此时算出了 1 面 的面积

扫描第二条线的时候,它是一条下线,会把区间长度更新为 15,注意,获取长度的操作是利用线段树中线段端点差值来获取的

而不是通过插入的线段的值,所以更新之后获取的长度是 15,而不是叠加的20。

面积 += 2线与3线的高度差 * tree[1].sum = 75

扫描第三条线的时候发现是 上线,更新完区间之后,有效长度为 10

计算出最后一块面积55

所以总面积是 180

最需要注意的就是区间问题,一般的线段树区间是分成 [1,2][3,4][4,5]这样子的,但是这样会丢失区间

比如我们需要 [2,3]的时候就找不到这个区间了,所以扫面线的线段树需要改造

我们把 [0,0] 表示为线段 [0,1]   [1,1]表示线段 [1,2]    [0,3]表示线段 [0,4]

所以在插入线段的时候 要插入  ( l , r -1 ) , 计算区间的时候 用 pos[r + 1] - pos[l] 就可以了

贴上代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

#define maxn 100005
#define mem(a,x) memset(a,x,sizeof(a))
#define debug printf("********");
struct line{
	double l,r,h;
	int flag;
	line(){}
	line(double ll,double rr,double hh,int fflag)
	 : l(ll),r(rr),h(hh),flag(fflag){}
}lines[maxn];

struct node{
	int l,r,date;
	double sum;
}tree[5000];

int lazy[maxn * 4];
double pos[maxn];

bool cmp(line a,line b){
	return a.h < b.h;
}

void push_up(int root){
	if(lazy[root] > 0){
		tree[root].sum = pos[tree[root].r + 1] - pos[tree[root].l];
	}	
	else if(tree[root].l == tree[root].r){
		tree[root].sum = 0;
	}else{
		tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
	}
}

void build_tree(int root,int l,int r){
	tree[root].l = l;
	tree[root].r = r;
	tree[root].sum = 0;
	if(l == r)
		return ;
	int mid = (l + r) >> 1;
	build_tree(root << 1,l,mid);
	build_tree(root << 1 | 1,mid + 1,r);
}

void update(int root,int l,int r,int val){
	if(tree[root].l >= l && tree[root].r <= r){
		lazy[root] += val;
		push_up(root);
		return ;
	}
	int mid = (tree[root].l + tree[root].r) >> 1;
	if(l <= mid)
		update(root << 1,l,r,val);
	if(r > mid)
		update(root << 1 | 1,l,r,val);
	push_up(root);
}

int discre(int n){
	int tmp = 1;
	for(int i = 1;i < n;i++){
		if(pos[i] != pos[i - 1]){
			pos[tmp++] = pos[i];
		}
	}
	return tmp;
}
int main(){
	int n,cnt,num,cas = 1;
	double x1,y1,x2,y2;
	while(scanf("%d",&n) != EOF){
		if(n == 0)
			break;
		cnt = num = 0;
		mem(lazy,0);
		for(int i = 1;i <= n;i++){
			scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
			lines[cnt++] = line(x1,x2,y1,1);
			lines[cnt++] = line(x1,x2,y2,-1); 
			pos[num++] = x1;
			pos[num++] = x2; 
		}
		sort(lines,lines + cnt,cmp);
		sort(pos,pos + num);
		num = discre(num);
		double area = 0.0;
		build_tree(1,0,num - 1);
		for(int i = 0;i < cnt - 1;i++){
			int l = lower_bound(pos,pos + num,lines[i].l) - pos;
			int r = lower_bound(pos,pos + num,lines[i].r) - pos - 1;
			update(1,l,r,lines[i].flag);
			area += (lines[i + 1].h - lines[i].h) * tree[1].sum;
		}
		printf("Test case #%d\n",cas++);
		printf("Total explored area: %.2f\n\n",area);
	}
}
<think> 嗯...用户这次问的是扫描线算法中线段树是否动态开点。看来用户正在学习计算几何的高级数据结构应用,可能是个OI选手或者算法爱好者。 从用户引用的两篇文献来看,第一篇详细描述了扫描线配合线段树求矩形面积并的标准流程,第二篇则涉及矩形覆盖最大点数的变种问题。用户能提出"动态开点"这样的专业术语,说明已经对线段树的常见优化方式有所了解。 关于动态开点问题,需要分场景讨论:在标准的离散化扫描线中,由于横坐标已离散化为固定区间,线段树可以预先建树无需动态开点;但在处理浮点数或值域过大的情况时,动态开点就成为必要选择。这个区别很关键,用户可能正在处理特殊场景才会这么问。 考虑到用户之前关注过线段树维护区间覆盖的技术细节,后续可以补充离散化与动态开点的性能对比。不过当前问题需要先明确回答核心疑问——常规实现不用动态开点,特殊情况才需要。 </think> 在扫描线算法中,线段树**通常不需要动态开点**,但具体实现取决于问题需求和坐标系特征: ### 📍 标准实现(非动态开点) 1. **离散化处理** 扫描线算法的关键步骤是将所有坐标点在 $x$$-轴上离散化(如图1所示): $$ \text{步骤:收集坐标} \rightarrow \text{排序} \rightarrow \text{去重} \rightarrow \text{建立映射} $$ 离散化后,线段树管理的区间数量被压缩为 $O(n)$ 级别($n$ 为坐标点数),因此可直接**预建满二叉树**,无需动态开点[^1]。 2. **空间复杂度优化** 预建树的空间复杂度为 $O(n)$,例如: - 原始坐标范围:$[1, 10^9]$ - 离散化后区间数:$2000$(若 $n=1000$) 此时只需开 $4 \times 2000 = 8000$ 的节点数组,完全可行[^1]。 3. **维护信息** - 节点存储 **区间覆盖次数** 和 **有效长度** - 更新操作:区间加减(矩形入边 `+1`,出边 `-1`) - 查询操作:直接取根节点有效长度 ### ⚡ 需动态开点的情况 | **场景** | **原因** | **示例** | |-----------------------|--------------------------------------------------------------------------|---------------------------| | **值域极大且无法离散化** | 浮点数坐标或值域超限(如 $[1, 10^{18}]$) | 地理信息系统(GIS)中的浮点计算 | | **强制在线查询** | 矩形边界实时插入,无法预离散化 | 流式数据处理场景 | | **内存严格受限** | $n$ 极大(如 $>10^6$),离散化后区间仍过多,需节省空间 | 嵌入式设备上的算法部署 | ### 🌰 实例对比 1. **标准问题(矩形面积并)** ```python # 离散化坐标 xs = sorted(set(x_coords)) tree = SegmentTree([0]*4*len(xs)) # 预建树 ``` 2. **动态开点问题(浮点数覆盖)** ```python class Node: def __init__(self): self.cover = 0 self.len = 0.0 self.lson = None # 动态创建子节点 self.rson = None ``` ### ✅ 结论 - **大多数场景**(如引用[1][2]的矩形问题):离散化 + 静态线段树 ✅ - **特殊场景**(浮点数/在线查询):动态开点线段树 ✅ 若处理整数坐标系且可离散化,推荐静态实现,效率更高且编码简单[^1][^2]。 --- ### ❓相关问题 1. 扫描线算法中如何处理浮点数坐标的精度问题? 2. 离散化过程如何避免哈希冲突导致的查询错误? 3. 动态开点线段树在扫描线中的更新操作时间复杂度如何保证? 4. 矩形覆盖最大值问题(如HDU 5091)如何转化为扫描线模型?[^2] [^1]: 线段树应用——扫描线,离散化坐标与静态建树 [^2]: HDU 5091 矩形覆盖问题中的扫描线转化技巧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值