POJ1389[Area of Simple Polygons]

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

 Status

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



一道裸的扫描线矩形面积并,看数据范围应该不用离散化,管它的,写了好一点。

把矩形拆成左右两条线段,然后排个序,从左往右扫一道,再次巩固一下。

(我的线段树版本,每个节点记录的是[l,r]的里面线段的左端点,所以更新的时候更新[l,r-1]左端点呀~)

最主要的是巩固特殊的pushup

void pushup(int i)
{
	int l=T[i].l;
	int r=T[i].r;
	for(int j=0;j<=k;j++)T[i].len[j]=0;
	if(l==r)
	{
		int t=min(T[i].flag,k);
		T[i].len[t]=det[l+1]-det[l];
	}
	else
	{
		for(int j=0;j<=k;j++)
		{
			int t=min(T[i].flag+j,k);
			T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
		}
	}
}


用这个特殊的pushup更新被覆盖k的矩形面积,这样以后就算写覆盖多少次也没关系,直接套。


AC代码:

#include<cstdio>
#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<vector>
const int maxn=1000+20;
const int k=1;
using namespace std;

struct line
{
	int x,y1,y2,flag;
	line(int x,int y1,int y2,int flag)
	{
		this->x=x;
		this->y1=y1;
		this->y2=y2;
		this->flag=flag;
	}
	bool operator<(const line &p)const
	{
		return x<p.x;
	}
};
vector<line>L;
int det[2*maxn];
int tot;
struct node
{
	int l,r,flag;
	int len[5];
	void set(int l,int r,int flag)
	{
		this->l=l;
		this->r=r;
		this->flag=flag;
	}
}T[8*maxn];
void pushup(int i)
{
	int l=T[i].l;
	int r=T[i].r;
	for(int j=0;j<=k;j++)T[i].len[j]=0;
	if(l==r)
	{
		int t=min(T[i].flag,k);
		T[i].len[t]=det[l+1]-det[l];
	}
	else
	{
		for(int j=0;j<=k;j++)
		{
			int t=min(T[i].flag+j,k);
			T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
		}
	}
}
void build(int i,int l,int r)
{
	T[i].set(l,r,0);
	if(l==r)
	{
		pushup(i);
		return ;
	}
	int mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	pushup(i);
}
void updata(int i,int L,int R,int x)
{
	int l=T[i].l;
	int r=T[i].r;
	if(l>=L&&r<=R)
	{
		T[i].flag+=x;
		pushup(i);
		return;
	}
	int mid=(l+r)>>1;
	if(L<=mid)updata(i<<1,L,R,x);
	if(R>mid)updata(i<<1|1,L,R,x);
	pushup(i);
}
int find(int x)
{
	return lower_bound(det+1,det+tot+1,x)-det;
}
int main()
{
	int x1,y1,x2,y2;
	while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
	{
		tot=0;
		L.clear();
		if(x1==-1&&y1==-1&&x2==-1&&y2==-1)break;
		det[++tot]=y1;
		det[++tot]=y2;
		L.push_back(line(x1,y1,y2,1));
		L.push_back(line(x2,y1,y2,-1));
		while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
		{
			if(x1==-1&&y1==-1&&x2==-1&&y2==-1)break;
			det[++tot]=y1;
		    det[++tot]=y2;
	    	L.push_back(line(x1,y1,y2,1));
	    	L.push_back(line(x2,y1,y2,-1));
		}
		
		sort(det+1,det+tot+1);
		int t=tot;
		tot=unique(det+1,det+t+1)-det-1;
		sort(L.begin(),L.end());
		build(1,1,tot);
		
		int ans=0;
		for(int i=0;i<L.size()-1;i++)
		{
			int l=find(L[i].y1);
			int r=find(L[i].y2);
			updata(1,l,r-1,L[i].flag);
			ans+=T[1].len[k]*(L[i+1].x-L[i].x);
		}
		printf("%d\n",ans);
	}
	
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值