POJ 1389 求矩形面积并(线段树)

本文介绍了一种计算多个矩形在二维平面上所形成简单多边形总面积的方法。通过扫描线算法结合线段树数据结构,实现了高效计算。文章详细解析了算法步骤,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3436 Accepted: 1771

Description

There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates.

Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point.

Example: Consider the following three rectangles:

rectangle 1: < (0, 0) (4, 4) >,

rectangle 2: < (1, 1) (5, 2) >,

rectangle 3: < (1, 1) (2, 5) >.

The total area of all simple polygons constructed by these rectangles is 18.

Input

The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.

Output

For each test case, output the total area of all simple polygons in a line.

Sample Input

0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1  

Sample Output

18
10 

Source

Taiwan 2001
题意:给出若干矩形的对角线的坐标;求出矩形面积并;
解题思路:我们首先想到扫描线算法,利用计算机只能处理离散量的特性,所以我们将矩形离散成事件点:矩形的左,右两边的横坐标,纵坐标,另外,纵坐标是需要离散化的;扫描线第一次扫描到左边时,将这一段长度加入用线段树维护的数组中,这样可以保证不遗漏任何面积,也不会重复计算面积,扫到右边时,删除线段;
代码:
#include<stdio.h>
#include<memory.h>
#include<algorithm>
#define MAXN 11000
using namespace std;
struct Rect{
	long long x1,x2,y1,y2;
};
struct event{
	long long x,y1,y2;
	long long add;
};
struct segtree{
	long long count,total;//count表示当前线段树中还有多少个矩形,total表示扫描线的长度; 
};
long long n=0;
Rect rect[MAXN];
event evi[2*MAXN];
segtree tree[2*MAXN];
long long id[2*MAXN];
bool cmp(event a,event b)
{
	return a.x<b.x;
}
void up(long long i,long long lb,long long rb)
{
	tree[i].total=tree[i*2].total+tree[i*2+1].total;
	if(tree[i].count)
	tree[i].total=abs(id[rb]-id[lb]);
}
void ins(long long i,long long lb,long long rb,long long a,long long b,long long k)//线段树更新 
{
	if(lb==a&&rb==b)
	{
		tree[i].count+=k;
		up(i,lb,rb);
		return;
	}
	long long med=(lb+rb)/2;
	if(b<=med)
	ins(2*i,lb,med,a,b,k);
	else if(a>=med)
	ins(2*i+1,med,rb,a,b,k);
	else
	{
		ins(2*i,lb,med,a,med,k);
		ins(2*i+1,med,rb,med,b,k);
	}
	up(i,lb,rb);
}
long long area()
{
	for(long long i=0;i<n;i++)
	{
		id[i]=rect[i].y1;
		id[i+n]=rect[i].y2;
	}
	sort(id,id+2*n);
	for(long long i=0;i<2*n;i++)//离散化 
	{
		rect[i].y1=lower_bound(id,id+2*n,rect[i].y1)-id;
		rect[i].y2=lower_bound(id,id+2*n,rect[i].y2)-id;
	}
	for(long long i=0;i<n;i++)
	{
		evi[i].add=1;
		evi[i+n].add=-1;
		evi[i].x=rect[i].x1;
		evi[i+n].x=rect[i].x2;
		evi[i].y1=evi[i+n].y1=rect[i].y1;
		evi[i].y2=evi[i+n].y2=rect[i].y2;
	}
	sort(evi,evi+n*2,cmp);
	long long ans=0;
	for(long long i=0;i<2*n;i++)
	{
		if(i>0&&evi[i].x>evi[i-1].x)
		ans+=(evi[i].x-evi[i-1].x)*tree[1].total;
		long long a=min(evi[i].y1,evi[i].y2);
		long long b=max(evi[i].y1,evi[i].y2);
		ins(1,0,2*n-1,a,b,evi[i].add);
	}
	return ans;
}
int main()
{
	long long a,b,c,d,i=0;
	bool flag;
	while(1)
	{
		memset(tree,0,sizeof(tree));
		memset(rect,0,sizeof(rect));
		memset(evi,0,sizeof(evi));
		memset(id,0,sizeof(id));
		n=0;i=0;
		flag=false;
		while(1)
		{
			scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
			if(a==-1&&!flag)
			{
				return 0;
			}			
			else if(a==-1)
			break;
			else
			{
				rect[i].x1=a;
				rect[i].y1=b;
				rect[i].x2=c;
				rect[i++].y2=d;
				n++;
				flag=true;
			}
		}
		printf("%lld\n",area());
	}
	return 0;
}

hdoj 1264也是一道类似的,但那道题卡内存,所以要对以上代码进行内存优化;但是我还没有找到方法;所以欢迎大家评论指导。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值