poj 1389 Area of Simple Polygons(线段树+扫描线)
2016-08-09 08:02
393 查看
Area of Simple Polygons
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 Taiwan 2001 |
[Discuss]
题解:线段树+扫描线
将矩形的左右边界按横坐标排序,对于纵坐标用线段树维护被覆盖的点数。
但是覆盖的时候不标记下放,这样小线段还保留着之前的结果,所以删线段的时候不会影响之前的小线段,就可以直接用小线段更新答案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100000
#define LL long long
using namespace std;
int tree[N*4],delta[N*4],tot,cnt;
LL len[N*4];
struct data
{
int l,r,x,pd;
}a
;
void update(int now,int l,int r)
{
if (tree[now]>0)
len[now]=(LL)r-l+1;
else if (l==r) len[now]=0;
else len[now]=len[now<<1]+len[now<<1|1];
}
void qjchange(int now,int l,int r,int ll,int rr,int v)
{
if (l>=ll&&r<=rr)
{
tree[now]+=v; update(now,l,r);
return;
}
int mid=(l+r)/2;
if (ll<=mid) qjchange(now<<1,l,mid,ll,rr,v);
if (rr>mid) qjchange(now<<1|1,mid+1,r,ll,rr,v);
update(now,l,r);
}
int cmp(data a,data b)
{
return a.x<b.x;
}
int main()
{
freopen("a.in","r",stdin);
int x,y,x1,y2;
while (scanf("%d%d%d%d",&x,&y,&x1,&y2)!=EOF)
{
if (x==-1) break;
int maxn=0;
memset(tree,0,sizeof(tree));
memset(len,0,sizeof(len));
cnt=0;
cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x; a[cnt].pd=1;
cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x1; a[cnt].pd=-1;
maxn=max(maxn,y2);
while(scanf("%d%d%d%d",&x,&y,&x1,&y2))
{
if (x==-1) break;
cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x; a[cnt].pd=1;
cnt++; a[cnt].l=y; a[cnt].r=y2; a[cnt].x=x1; a[cnt].pd=-1;
maxn=max(maxn,y2);
}
sort(a+1,a+cnt+1,cmp);
LL ans=0;
for (int i=1;i<=cnt;i++)
{
if (i!=1) ans+=(LL)(a[i].x-a[i-1].x)*len[1];
qjchange(1,0,maxn,a[i].l+1,a[i].r,a[i].pd);
}
printf("%lld\n",ans);
}
}
相关文章推荐
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线)
- poj 1389 Area of Simple Polygons
- POJ 1389 Area of Simple Polygons(线段树+扫描线求矩形面积并)
- POJ-1389-Area of Simple Polygons
- poj - 1389 - Area of Simple Polygons(离线化+扫描线+线段树)
- poj 1389 Area of Simple Polygons
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)
- POJ 1389 Area of Simple Polygons
- 【POJ】1389 Area of Simple Polygons
- POJ 1389 Area of Simple Polygons
- [POJ1389]Area of Simple Polygons(扫描线+线段树)
- POJ-1389-Area of Simple Polygons
- POJ 1389 Area of Simple Polygons(线段树+扫描面积)
- POJ 1389 Area of Simple Polygons(面积合并,线段树+离散化)
- POJ 1389 Area of Simple Polygons
- poj 1389 Area of Simple Polygons
- poj 1389 Area of Simple Polygons(线段树做法)
- POJ 1389 Area of Simple Polygons(多矩形重叠面积==离散化)
- POJ1389-Area of Simple Polygons
- POJ 1389 Area of Simple Polygons(线段树+扫描线+离散化)