POJ 1151 Atlantis (扫描线+线段树)

这是一道模板题,描述了一种叫扫描线的算法

题意就是给出一个坐标系和 n n n 个矩形,求面积并,如下图:

在这里插入图片描述

矩形有凹凸不是太好直接算出

试想,如果我们用一条竖线从左到右扫过坐标系,那么直线上的并集图形覆盖长度只会在每个矩形左右边界处发生变化

换言之,其实就是 2 ∗ n 2*n 2n 个线段,每一段在直线上覆盖的长度固定,因此此块的面积就是 L ∗ L* L 该段的宽度,各段面积之和即为所求,这条直线称为扫描线,我们称这个算法叫扫描线算法

具体来说,我们可以取 n n n 个矩形的左右边界,若一个矩阵的两个对角顶点坐标为 ( x 1 , y 1 ) (x_1,y_1) (x1,y1) ( x 2 , y 2 ) (x_2,y_2) (x2,y2),不妨设 x 1 &lt; x 2 x_1&lt;x_2 x1<x2 y 1 &lt; y 2 y_1&lt;y_2 y1<y2,则左边界为四元组 ( x 1 , y 1 , y 2 , 1 ) (x_1,y_1,y_2,1) (x1,y1,y2,1),右边界为 ( x 2 , y 1 , y 2 , − 1 ) (x_2,y_1,y_2,-1) (x2,y1,y2,1),不妨把这些四元组按照 x x x 递增排序,如图所示(比较模糊,见谅):

在这里插入图片描述
显然本题需要对坐标进行离散化,设 v a l ( y ) val(y) val(y) 表示 y y y 被离散化后对应的数值, r a w ( x ) raw(x) raw(x) 表示 x x x 对应的原数

离散化后,假设有 m m m 个数值,分别对应 r a w ( 1 ) , r a w ( 2 ) , . . . r a w ( m ) raw(1),raw(2),...raw(m) raw(1),raw(2),...raw(m),则扫描线至多被分成 m − 1 m-1 m1 段,第 i i i 段为 [ r a w ( i ) , r a w ( i + 1 ) ] [raw(i),raw(i+1)] [raw(i),raw(i+1)]

建立数组 c [ i ] c[i] c[i] 表示第 i i i 段被覆盖的次数,初始 c c c 数组为 0 0 0

对于一个排序完后的数组 ( x , y 1 , y 2 , k ) (x,y_1,y_2,k) (x,y1,y2,k),他会对 c [ v a l ( y 1 ) ] , c [ v a l ( y 1 ) + 1 ] , . . . , c [ v a l ( y 2 ) − 1 ] c[val(y_1)],c[val(y_1)+1],...,c[val(y_2)-1] c[val(y1)],c[val(y1)+1],...,c[val(y2)1] 都加 k k k,相当于覆盖 [ y 1 , y 2 ] [y_1,y_2] [y1,y2] 区间,此时如果下一个四元组横坐标为 x 2 x_2 x2,那么扫描线从 x x x 扫到 x 2 x_2 x2 时,被覆盖的长度固定为 ∑ c [ i ] &gt; 0 ( r a w ( i + 1 ) − r a w ( i ) ) \sum\limits_{c[i]&gt;0}(raw(i+1)-raw(i)) c[i]>0(raw(i+1)raw(i)),即数组 c c c 中至少被覆盖一次的“段”的长度

于是,我们就让最终的答案 a n s ans ans 累加上 ( x 2 − x ) ∗ ∑ c [ i ] &gt; 0 ( r a w ( i + 1 ) − r a w ( i ) ) (x_2-x)*\sum\limits_{c[i]&gt;0}(raw(i+1)-raw(i)) (x2x)c[i]>0(raw(i+1)raw(i))

对于每个四元组,采用暴力在 c c c 数组上执行修改与统计,即可在 O ( n 2 ) O(n^2) O(n2) 的时间复杂度内完成

值得说明的是,四元组 ( y 1 , y 2 ) (y_1,y_2) (y1,y2) 都是坐标,是一个点

我们需要维护的是扫描线上每一段被覆盖的次数及其长度,对“点”的覆盖次数进行统计是没有意义的

因为我们把 c c c 数组中的每一个值定义成扫描线上一个区间的覆盖次数,四元组 ( x , y 1 , y 2 , k ) (x,y_1,y_2,k) (x,y1,y2,k) c [ v a l ( y 1 )   v a l ( y 2 ) − 1 ] c[val(y_1)~val(y_2)-1] c[val(y1) val(y2)1] 产生影响。读者在解题时一定要注意此类边界情况

通过线段树维护 c c c 数组,使得复杂度为 O ( n   l o g   n ) O(n~log~n) O(n log n)

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 410 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int tag[N << 2] ;
double sum[N << 2], X[N] ;
int n, num, Case ;

struct node {
	double l, r, y ; int fl ;
	bool operator < (node a) const {
		return y < a.y ;
	}
} l[N] ;

#define ls(x) x << 1
#define rs(x) x << 1 | 1

void pushup(int x, int l, int r) {
	if (tag[x]) sum[x] = X[r + 1] - X[l] ;
	else if (l == r) sum[x] = 0 ;
	else sum[x] = sum[ls(x)] + sum[rs(x)] ;
}

void modify(int x, int l, int r, int L, int R, int c) {
	if (L > r || R < l) return ;
	if (L <= l && r <= R) {
		tag[x] += c ;
		pushup(x, l, r) ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	modify(ls(x), l, mid, L, R, c) ;
	modify(rs(x), mid + 1, r, L, R, c) ;
	pushup(x, l, r) ;
}

signed main(){
	while (scanf("%d", &n) != EOF && n) {
		clr(tag) ; clr(sum) ; num = 0 ;
		rep(i, 1, n) {
			double x1, x2, y1, y2 ;
			scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2) ;
			l[++num] = (node) {x1, x2, y1, 1} ; X[num] = x1 ;
			l[++num] = (node) {x1, x2, y2, -1} ; X[num] = x2 ;
		}
		sort(l + 1, l + num + 1) ; sort(X + 1, X + num + 1) ;
		double ans = 0 ;
		int tt = unique(X + 1, X + num + 1) - (X + 1) ;
		rep(i, 1, num - 1) {
			int ll = lb(X + 1, X + tt + 1, l[i].l) - X, rr = lb(X + 1, X + tt + 1, l[i].r) - (X + 1) ;
			modify(1, 1, tt, ll, rr, l[i].fl) ;
			ans += sum[1] * (l[i + 1].y - l[i].y) ;
		}
		printf("Test case #%d\n", ++Case) ;
		printf("Total explored area: %.2f\n\n", ans) ;
	}
	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值