您的位置:首页 > 编程语言 > Go语言

POJ 1389 Area of Simple Polygons(线段树+扫描线+离散化)

2017-07-27 18:54 579 查看
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, th
dd8b
e 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


题解:

扫描线的裸题。。不过这题好像数据范围比较小不用离散化。。反正用都用了,求面积的模板套的去,与一题写法几乎一样。。这里就不解释了

如果不懂原理看我之前的文章:线段树扫描线学习

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
struct node
{
int l,r;
int len;
int s;
}t[2005*4];
int x[2005];//储存离散后的端点
struct bian
{
int l,r;//线段左右端点
int h;//线段高度
int d;//线段属性,上底还是下底,下底为1,上底为-1
}a[2005];
int cmp(bian x,bian y)//排序使从下到上扫描
{
return x.h<y.h;
}
void Build(int l,int r,int k)
{
t[k].l=l;
t[k].r=r;
t[k].len=0;
t[k].s=0;
if(l==r)
return;
int mid=(l+r)/2;
Build(l,mid,k*2);
Build(mid+1,r,k*2+1);
}
void pushup(int k)
{
if(t[k].s)//如果全部覆盖
{
t[k].len=x[t[k].r+1]-x[t[k].l];
}
else if(t[k].l==t[k].r)//如果为点
t[k].len=0;
else//如果不完全覆盖
{
t[k].len=t[k*2].len+t[k*2+1].len;
}
}
void update(int l,int r,int k,int d)//日常更新
{
if(t[k].l==l&&t[k].r==r)
{
t[k].s+=d;
pushup(k);
return;
}
int mid=(t[k].l+t[k].r)/2;
if(r<=mid)
update(l,r,k*2,d);
else if(l>mid)
update(l,r,k*2+1,d);
else
{
update(l,mid,k*2,d);
update(mid+1,r,k*2+1,d);
}
pushup(k);
}
int main()
{
int i,j,k,n,m,x1,x2,y1,y2,tot=0;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
break;
a[tot].l=x1;
a[tot+1].l=x1;
a[tot].r=x2;
a[tot+1].r=x2;
a[tot].h=y1;
a[tot+1].h=y2;
a[tot].d=1;
a[tot+1].d=-1;
x[tot]=x1;
x[tot+1]=x2;
tot+=2;//记录各种信息
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
{
sort(a,a+tot,cmp);
sort(x,x+tot);//排序为了离散化
k=unique(x,x+tot)-x;//离散化去重
Build(0,k-1,1);
int s=0;
for(i=0;i<tot;i++)
{
int l=lower_bound(x,x+k,a[i].l)-x;
int r=lower_bound(x,x+k,a[i].r)-x-1;//查找线段端点出现的下标
update(l,r,1,a[i].d);
s+=t[1].len*(a[i+1].h-a[i].h);//高度差乘上区间中的线段长
}
printf("%d\n",s);
break;
}
a[tot].l=x1;
a[tot+1].l=x1;
a[tot].r=x2;
a[tot+1].r=x2;
a[tot].h=y1;
a[tot+1].h=y2;
a[tot].d=1;
a[tot+1].d=-1;
x[tot]=x1;
x[tot+1]=x2;
tot+=2;
}
tot=0;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: