poj 1389 Area of Simple Polygons(线段树+扫描线)

本文介绍了一种计算多个矩形并集所形成简单多边形最大面积的算法。通过线段树和扫描线技术,文章详细阐述了如何处理矩形边界以构建简单多边形,并给出具体实现代码。

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

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3499 Accepted: 1805

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

[Submit]   [Go Back]   [Status]   [Discuss]



题解:线段树+扫描线

 将矩形的左右边界按横坐标排序,对于纵坐标用线段树维护被覆盖的点数。

但是覆盖的时候不标记下放,这样小线段还保留着之前的结果,所以删线段的时候不会影响之前的小线段,就可以直接用小线段更新答案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100000
#define LL long long
using namespace std;
int tree[N*4],delta[N*4],tot,cnt;
LL len[N*4];
struct data
{
	int l,r,x,pd;
}a[N];
void update(int now,int l,int r)
{
	if (tree[now]>0)
	 len[now]=(LL)r-l+1;
	else  if (l==r)  len[now]=0;
    else len[now]=len[now<<1]+len[now<<1|1]; 
}
void qjchange(int now,int l,int r,int ll,int rr,int v)
{
	if (l>=ll&&r<=rr)
	 {
	 	tree[now]+=v; update(now,l,r);
	 	return;
	 }
	int mid=(l+r)/2;
	if (ll<=mid) qjchange(now<<1,l,mid,ll,rr,v);
	if (rr>mid) qjchange(now<<1|1,mid+1,r,ll,rr,v);
	update(now,l,r);
}
int cmp(data a,data b)
{
	return a.x<b.x;
}
int main()
{
	freopen("a.in","r",stdin);
	int x,y,x1,y2;
	while (scanf("%d%d%d%d",&x,&y,&x1,&y2)!=EOF)
	{
		if (x==-1)  break;
		int maxn=0;
		memset(tree,0,sizeof(tree));
		memset(len,0,sizeof(len));
		cnt=0;
		cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x; a[cnt].pd=1;  
		cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x1; a[cnt].pd=-1;
		maxn=max(maxn,y2);
		while(scanf("%d%d%d%d",&x,&y,&x1,&y2))
		 {
		 	if (x==-1)  break;
		 	cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x; a[cnt].pd=1;
		    cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x1; a[cnt].pd=-1;
		    maxn=max(maxn,y2);
		 }
		sort(a+1,a+cnt+1,cmp);
		LL ans=0;
		for (int i=1;i<=cnt;i++)
		 {
		    if (i!=1)  ans+=(LL)(a[i].x-a[i-1].x)*len[1];
			qjchange(1,0,maxn,a[i].l+1,a[i].r,a[i].pd);	
		 }
		printf("%lld\n",ans);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值