[POJ 1151] Atlantis【扫描线+离散化+线段树】

  • 2018-01-08
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 10000K

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

Solution:

扫描线的定义:https://en.wikipedia.org/wiki/Sweep_line_algorithm

我的第一道扫描线。。

二维扫描线算法经典入门题,矩形覆盖问题的模板。

我们只需要抽取所有水平的边,按纵坐标从小到大排序(当然也可以从大到小)。再利用差分的思想(类比一维扫描线),每个矩形区域下边权值赋为 1上边权值赋为 -1

将水平边的横坐标离散化处理,以一一对应线段树的下标。

按纵坐标从小到大扫描,每次将新的边界加入线段树中,维护当前水平方向覆盖长度,每次将该长度乘上竖直方向覆盖的长度(即本次边界与下次边界的纵坐标差),统计入答案即可。

注意由于这里的长度为实数,而普通的线段树维护的是整数区间,即:

  • [l, r] 在线段树上被分成 [l, m][m + 1, r],导致 (m, m + 1) 的长度丢失。

为了避免此类间隔区域的长度丢失,我们可以将线段树改为维护 [l, r + 1] 这个区间的长度。这样的话进行 add 操作时右端点应该减 1,否则最右端会多统计 1 个下标长度

注意每组数据后换行

Plus: 由于数据范围较小,这道题实际上可以直接将两个维度都离散化直接统计每一块是否被覆盖。。但这样复杂度就是 O(Tn2) 了。

普通的扫描线算法资料:

非常高深的区域填充的扫描线算法资料:

Code: O(Tnlogn) [180K, 16MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define MAX2N 202
#define eps 1e-8
using namespace std;

inline int dsgn(const double &x){
	if(fabs(x) <= eps) return 0;
	return x < 0 ? -1 : 1;
}

int T = 0, n, topd;

struct _double{
	double val;
	
	_double() {}
	_double(double val): val(val) {}
	
	inline bool operator < (const _double &d2) const {return dsgn(val - d2.val) < 0;}
	
	inline bool operator == (const _double &d2) const {return dsgn(val - d2.val) == 0;}
};  // Overload operator == for double comparison

_double disc[MAX2N];  // Discretization of the x-axis coordinate

struct Segment{
	double _l, _r, h;
	int l, r, v;
	
	inline bool operator < (const Segment &seg2) const {return h < seg2.h;}
} seg[MAX2N];

struct Node{
	int cov;
	double sum;
};

struct Segment_Tree{
	#define MAXNODE (MAX2N << 2)
	#define lch (u << 1)
	#define rch (u << 1 | 1)
	#define smid (l + r >> 1)
	Node node[MAXNODE];
	
	inline void clear() {memset(node, 0, sizeof(node));}
	
	inline void update(int u, int l, int r){
		if(node[u].cov) node[u].sum = disc[r + 1].val - disc[l].val;  // To avoid gap missing
		else if(l == r) node[u].sum = 0;
		else node[u].sum = node[lch].sum + node[rch].sum;
	}
	
	inline void add(int u, int l, int r, int al, int ar, int v){
		if(al <= l && r <= ar){
			node[u].cov += v;
			update(u, l, r);
			return;
		}
		if(al <= smid) add(lch, l, smid, al, ar, v);
		if(smid < ar) add(rch, smid + 1, r, al, ar, v);
		update(u, l, r);
	}
} sgt;

int main(){
	while(scanf("%d", &n) != EOF && n){
		for(register int i = 1; i <= n; i++){
			double lx, ly, rx, ry;
			scanf("%lf%lf%lf%lf", &lx, &ly, &rx, &ry);
			
			Segment &uped = seg[(i << 1) - 1], &dwned = seg[i << 1];
			uped.h = lx, uped._l = ly, uped._r = ry, uped.v = 1;
			dwned.h = rx, dwned._l = ly, dwned._r = ry, dwned.v = -1;
			
			disc[(i << 1) - 1].val = ly, disc[i << 1].val = ry;
		}
		n <<= 1, sort(disc + 1, disc + n + 1);
		topd = unique(disc + 1, disc + n + 1) - disc - 1;
		for(register int i = 1; i <= n; i += 2){
			seg[i].l = seg[i + 1].l = lower_bound(disc + 1, disc + topd + 1, _double(seg[i]._l)) - disc;
			seg[i].r = seg[i + 1].r = lower_bound(disc + 1, disc + topd + 1, _double(seg[i]._r)) - disc;
		}
		// Discretize the x-axis coordinate
		sort(seg + 1, seg + n + 1);
		double area = 0; sgt.clear();
		for(register int i = 1; i < n; i++){
			sgt.add(1, 1, topd, seg[i].l, seg[i].r - 1, seg[i].v);  // Upper bound decrease 1 for the right-extra interval !!!
			area += (seg[i + 1].h - seg[i].h) * sgt.node[1].sum; 
		}
		printf("Test case #%d\n", ++T);
		printf("Total explored area: %.2f\n\n", area);  // Beware of the presentation !!!
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址:
darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io


常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai