【POJ 1389】Area of Simple Polygons(线段树+扫描线)

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3500 Accepted: 1806

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]

Home Page   Go Back  To top

[题意][计算总面积,重叠部分只计算一次]

【题解】【扫描线+线段树】
【一般来说,扫描线都是用来计算矩形的周长、面积之类的东西】
【就本题来说,计算面积,将矩形按横坐标排序,线段树里记录纵坐标,用线段树维护被纵坐标覆盖的点数】
覆盖的时候不标记下放,这样小线段还保留着之前的结果,所以删线段的时候不会影响之前的小线段,就可以直接用小线段更新答案。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
struct data{
	int l,r,x,val;
}a[100010];
int sum[500010];
ll s[500010];
int tot;
int tmp(data a,data b)
{
	return a.x<b.x;
}
inline void updata(int now,int l,int r)
{
	if(sum[now]>0) s[now]=(ll)r-l+1;
	 else
	  if(l==r) s[now]=0;
	   else s[now]=s[(now<<1)]+s[(now<<1)|1];
	return;
}
void change(int now,int l,int r,int al,int ar,int v)
{
	if(al<=l&&r<=ar)
	 {
	 	sum[now]+=v; updata(now,l,r);
	 	return;
	 }
	int mid=(l+r)>>1;
	if(al<=mid) change((now<<1),l,mid,al,ar,v);
	if(ar>mid) change((now<<1)|1,mid+1,r,al,ar,v);
	updata(now,l,r);
}
int main()
{
	int i,j;
	int x1,y1,x2,y2;
	while((scanf("%d%d%d%d",&x1,&y1,&x2,&y2)==4))
	 {
	 	int maxn=0; tot=0;
		if(x1==-1) break; 
	 	memset(sum,0,sizeof(sum));
	 	tot++; a[tot].l=y1,a[tot].r=y2; a[tot].x=x1; a[tot].val=1;
	 	tot++; a[tot].l=y1;a[tot].r=y2; a[tot].x=x2; a[tot].val=-1;
	 	if(maxn<y2) maxn=y2;
	 	while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)==4)
	 	 {
	 	 	if(x1==-1) break;
	 	    tot++; a[tot].l=y1; a[tot].r=y2; a[tot].x=x1; a[tot].val=1;
	 	    tot++; a[tot].l=y1; a[tot].r=y2; a[tot].x=x2; a[tot].val=-1;
	 	    if(maxn<y2) maxn=y2;
		  }
		sort(a+1,a+tot+1,tmp);
		ll ans=0;
		for(i=1;i<=tot;++i)
		 {
		 	if(i!=1) ans+=(ll)(a[i].x-a[i-1].x)*s[1];
		 	change(1,0,maxn,a[i].l+1,a[i].r,a[i].val);
		 }
		printf("%lld\n",ans);
	 }
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值