线段树-面积并

第一次学这个的时候,说实话很头疼,看别人写的文章也学不会,花了一下午才有了些头绪。所谓的面积并,就是求多个矩形相交的面积或者求不相交部分的面积。这类问题要用到一个概念,叫做“扫描线”。刚开始完全看不懂,觉得自己太笨了。刚练习时的时候,我是做的一道杭电上的题目。HDU 1542

Atlantis

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坐标范围来确定的。而线段树的各节点的值表示的是这个范围内的平行于X轴的线段长度。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define  lson l, m, rt<<1
#define  rson m+1, r, rt<<1|1

const int maxn = 2222;
int cnt[maxn<<2];
double sum[maxn << 2];		//对线段长度建树,初始都为0
double X[maxn];		//存放各个平行于X轴的线段的横坐标

/************************************************************************/
/*                                                                     
扫描线法:假想有一条扫描线,从左往右(从右往左),或者从下往上(从上往下)扫描过整个多边形(或者说畸形。。多个矩形叠加后的那个图形)。
如果是竖直方向上扫描,则是离散化横坐标,如果是水平方向上扫描,则是离散化纵坐标。下面的分析都是离散化横坐标的,并且从下往上扫描的。
扫描之前还需要做一个工作,就是保存好所有矩形的上下边。
并且按照它们所处的高度进行排序,另外如果是上边我们给他一个值-1,下边给他一个值1,我们用一个结构体来保存所有的上下边
*/
/************************************************************************/

class CSeg
{
public:
	double h, l, r;		//l, r表示这条边上下边的左右坐标,h是这条边所处的高度
	int s;		//所赋的值,为1或-1
	CSeg(){};
	CSeg(double a, double b, double c, int d) : l(a), r(b), h(c), s(d){}
	bool operator < (const CSeg &cmp) const		//根据每条线段的高度,从小到大排序
	{
		return h < cmp.h;
	}
};
CSeg ss[maxn];
 
void PushUP(int rt, int l, int r)
{
	if (cnt[rt])
		sum[rt] = X[r + 1] - X[l];	//如果cnt[rt]不为0,计算sum[rt]为线段在区域[l,r]的长度
	else if (l == r)
		sum[rt] = 0;
	else
		sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void updata(int L, int R, int c, int l, int r, int rt)
{
	if (L <= l&&r <= R)
	{
		cnt[rt] += c;
		PushUP(rt, l, r);
		return;
	}
	int m = (l + r) >> 1;
	if (L <= m)
		updata(L, R, c, lson);
	if (R > m)
		updata(L, R, c, rson);
	PushUP(rt, l, r);
}

int Bin(double key, int n, double X[])		//二分查找值key在数组X中的下标
{
	int l = 0, r = n - 1;
	while (l <= r)
	{
		int m = (l + r) >> 1;
		if (X[m] == key)
			return m;
		if (X[m] < key)
			l = m + 1;
		else
			r = m - 1;
	}
	return -1;
}

int main(int argc, char *argv[])
{
	int n, cas = 1;
	while (~scanf_s("%d", &n) && n != 0)
	{
		int m = 0;
		while (n--)
		{
			double a, b, c, d;
			scanf_s("%lf %lf %lf %lf", &a, &b, &c, &d);
			X[m] = a;
			ss[m++] = CSeg(a, c, b, 1);
			X[m] = c;
			ss[m++] = CSeg(a, c, d, -1);
		}
		sort(X, X + m);
		sort(ss, ss + m);
		int k = 1;	//统计各线段横坐标的个数
		for (int i = 1; i < m; i++)
		{
			if (X[i] != X[i - 1])	//判断相邻线段的横坐标是否相等,去除相同的坐标
				X[k++] = X[i];
		}
		memset(cnt, 0, sizeof(cnt));
		memset(sum, 0, sizeof(sum));	//构建线段数,各节点区域初始面积为0
		double ret = 0;		//统计各矩形不相交区域面积的和
		for (int i = 0; i < m - 1; i++)		//从离X轴最近的线段开始扫描
		{
			int l = Bin(ss[i].l, k, X);
			int r = Bin(ss[i].r, k, X) - 1;		//查找ss[i]的区间[l,r]
			if (l <= r)
				updata(l, r, ss[i].s, 0, k - 1, 1);		//更新区间[l,r]
			ret += sum[1] * (ss[i + 1].h - ss[i].h);
		}
		for (int i = 0; i < 8; i++)
		{
			cout << sum[i] << ' ';
		}
		printf("Test case #%d\nTotal explored area: %.2lf\n\n", cas++, ret);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值