POJ1389Area of Simple Polygons【离散化+线段树+扫描线】

Language:
Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3304 Accepted: 1711

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 


题意:同poj1151

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define lson l,mid,rt<<1
#define rson mid,r,rt<<1|1 
#define MAX 1010
using namespace std;
struct Node{
	int flag;
	double l,r,h;
}A[MAX<<2];
int cover[MAX<<4];
double len[MAX<<4],num[MAX<<2],tmp[MAX<<2];
void init(){	
	memset(len,0,sizeof(len));
	memset(cover,0,sizeof(cover));
}
bool cmp(Node a,Node b){
	if(a.h==b.h)
		return a.flag<b.flag;
	return a.h<b.h;
}
void pushup(int l,int r,int rt){
	if(cover[rt])len[rt]=num[r]-num[l];
	else if(l+1==r)len[rt]=0;
	else len[rt]=len[rt<<1]+len[rt<<1|1];
}
void update(int a,int b,int c,int l,int r,int rt){
	if(a==l&&b==r){
		cover[rt]+=c;
		pushup(l,r,rt);
		return ;
	}
	int mid=(l+r)>>1;
	if(mid>=b)update(a,b,c,lson);
	else if(a>=mid)update(a,b,c,rson);
	else{
		update(a,mid,c,lson);
		update(mid,b,c,rson);
	}
	pushup(l,r,rt);
}
int search(double now,int n){
	int left=1,right=n,ans;
	while(left<=right){
		int mid=(left+right)>>1;
		if(num[mid]>=now){
			ans=mid;
			right=mid-1;
		}
		else{
			left=mid+1;
		}
	}
	return ans;
}
int main()
{
	int n=1,i,j,id=0;
	double x1,x2,y1,y2,ans;
	while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2),x1!=-1||x2!=-1||y1!=-1||y2!=-1){
		A[++id].l=x1;A[id].r=x2;A[id].h=y1;A[id].flag=1;tmp[id]=x1;
		A[++id].l=x1;A[id].r=x2;A[id].h=y2;A[id].flag=-1;tmp[id]=x2;
		while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2),x1!=-1||y1!=-1||x2!=-1||y2!=-1){
			A[++id].l=x1;A[id].r=x2;A[id].h=y1;A[id].flag=1;tmp[id]=x1;
			A[++id].l=x1;A[id].r=x2;A[id].h=y2;A[id].flag=-1;tmp[id]=x2;
			n++;
		}
		sort(A+1,A+id+1,cmp);
		sort(tmp+1,tmp+id+1);
		for(id=0,i=1;i<=(n<<1);++i){
			if(i==1||tmp[i]!=tmp[i-1])
				num[++id]=tmp[i];
		}
		init();A[0].h=A[1].h;ans=0;
		for(i=1;i<=(n<<1);++i){
			ans+=len[1]*(A[i].h-A[i-1].h);
			int l=search(A[i].l,id);
			int r=search(A[i].r,id);
			update(l,r,A[i].flag,1,id,1);
		}
		printf("%d\n",(int)ans);n=1;id=0;
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值