HDU-1542 Atlantis (线段树 求所有矩形面积和)

本文介绍了一种计算多个矩形覆盖总面积的方法,通过将y轴映射到线段树上并进行离散化处理,实现了高效计算所有矩形并集的面积。

Atlantis

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


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


#include <bits/stdc++.h>
using namespace std;
const int maxn = 101;
struct Xpoint{
	double x, l, r;
	int v;
	bool operator < (const Xpoint& e){
		return x < e.x;
	}
}line[maxn];
struct tree{
	int cover;
	double len;
}c[maxn << 4];
double y[maxn << 2];
void build(int o, int l, int r){
	c[o].cover = c[o].len = 0;
	if(l == r - 1) return;
	int mid = l + r >> 1;
	build(o << 1, l, mid);
	build(o << 1 | 1, mid, r);
}
void pushup(int o, int l, int r){
	if(c[o].cover > 0){
		c[o].len = y[r] - y[l]; return;
	}
	if(l == r - 1){
		c[o].len = 0; return;
	}
	c[o].len = c[o << 1].len + c[o << 1 | 1].len;
}
void add(int o, int l, int r, int L, int R, int v){
	if(l >= R || r <= L) return;
	if(l >= L && r <= R){
		c[o].cover += v;
		pushup(o, l, r);
		return;
	}
	int mid = l + r >> 1;
	add(o << 1, l, mid, L, R, v);
	add(o << 1 | 1, mid, r, L, R, v);
	pushup(o, l, r);
}
int main(){
	int n, casenum = 1;
	double x1, x2, y1, y2;
	while(scanf("%d", &n) != EOF){
		if(n == 0) return 0;
		int tot = 0;
		for(int i = 1; i <= n; ++i){
			scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
			line[++tot].x = x1;
			line[tot].l = y1;
			line[tot].r = y2;
			line[tot].v = 1;
			y[tot] = y1;

			line[++tot].x = x2;
			line[tot].l = y1;
			line[tot].r = y2;
			line[tot].v = -1;
			y[tot] = y2;
		}
		build(1, 1, tot);
		sort(y + 1, y + 1 + tot);
		sort(line + 1, line + 1 + tot);
		double ans = 0;
		for(int i = 1; i < tot; ++i){
			y1 = lower_bound(y + 1, y + 1 + tot, line[i].l) - y;
			y2 = lower_bound(y + 1, y + 1 + tot, line[i].r) - y;
			add(1, 1, tot, y1, y2, line[i].v);
		//	cout<<c[1].len<<endl;
			ans += c[1].len * (line[i + 1].x - line[i].x);
		}
		printf("Test case #%d\nTotal explored area: %.2lf\n\n", casenum++, ans);
	}
}

/*
题意:
100个矩形,坐标范围1e5,求所有矩形的总覆盖面积。

思路:
我们把y轴映射到线段树上,离散化一下,按照x的坐标从小到大依次处理所有矩形的平行于y轴的边。
每一个矩形x坐标小的边表示后面这个区域是被覆盖的,直到遇到x大的边结束。那么我们对于所有矩形
左边的边,我们讲其加到线段树的区间上,这样每处理一条边,区间上被覆盖的区间长度可以计算出来,
即这一段y轴的区域是被覆盖的,再利用x算一下这一段的面积。一直处理完所有面积即可。
*/


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值