POJ 1389 Area of Simple Polygons 线段树 扫描线

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3694 Accepted: 1914

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 

最开始搞成了面积交。。。

面积并怎么做呢?


觉得思想是很好的

我们考虑将每个矩形拆成两个线段

线段中维护矩形在x轴上的位置,纵坐标,及是上面还是下面

这样就可以维护矩形辣:

从下面进入矩形,统计答案,从上面退出矩形


所以我们按纵坐标对每条线段进行排序

动态维护每两个纵坐标中间的线段

对于两个纵坐标之间的答案就可以用线段覆盖长度的和乘纵坐标得到了

这个过程可以在离散化之后用线段树维护


有一点需要注意:

线段树维护过程中,如果天真的把[l,r]的线段长度做成x[r]-x[l]就大错特错辣

为什么呢?

考虑答案上传的时候

两段区间[l,mid],[mid+1,r]合并后的答案应该为x[r]-x[l]

可是如果向上面那样维护就会令答案变为x[r]-x[mid+1]+x[mid]-x[l]

诶?果然少了点什么吧~

所以肿么办呢

对于[l,r]的区间长度的赋值变为x[r+1]-x[l]

这样就解决了线段不连续的问题,在询问区间[l,r]的时候改为[l,r)就正确了


话说在POJ上scanf竟然比read快不知哪里去。。

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<bitset>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;

typedef double db;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return f*x;
}
inline void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=2010;

struct segment{int st,ed,y,f;friend bool operator <(const segment &x,const segment &y){return x.y<y.y;}}p[N];

int n,m,x[N<<1];

struct seg_tree{int w,sum;}tr[N<<1];

inline void pushup(int k,int l,int r)
{
	if(tr[k].w)tr[k].sum=x[r+1]-x[l];
	else if(l==r)tr[k].sum=0;
	else tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}

void update(int k,int l,int r,int x,int y,int f)
{
	if(l>=x&&r<=y){tr[k].w+=f;pushup(k,l,r);return ;}
	int mid=(l+r)>>1;
	if(x<=mid)update(k<<1,l,mid,x,y,f);
	if(y>mid)update(k<<1|1,mid+1,r,x,y,f);
	pushup(k,l,r);
}

inline int find(int k)
{
	register int l=1,r=m,mid;
	while(l<r)
	{
		mid=(l+r)>>1;
		x[mid]<k?l=mid+1:r=mid;
	}
	return l;
}

void initial()
{
	memset(x,0,sizeof(x));
	memset(tr,0,sizeof(tr));
}

int main()
{
	register int i,a,b,c,d,ans,l,r;
	while(1)
	{
		a=read();b=read();c=read();d=read();
		if(-1==a)break;
		n=m=ans=0;
		while(a!=-1)
		{
			p[++n]=(segment){a,c,b,1};x[n]=a;
			p[++n]=(segment){a,c,d,-1};x[n]=c;
			a=read();b=read();c=read();d=read();
		}
		sort(x+1,x+1+n);
		x[++m]=x[1];
		for(i=2;i<=n;++i)if(x[i]^x[m])x[++m]=x[i];
		sort(p+1,p+n+1);
		for(i=1;i<n;++i)
		{
			l=find(p[i].st);r=find(p[i].ed)-1;
			update(1,1,m,l,r,p[i].f);
			ans+=tr[1].sum*(p[i+1].y-p[i].y);
		}
		print(ans);puts("");
		initial();
	}
	return 0;
}
/*
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

18
10
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值