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

93 篇文章 1 订阅
52 篇文章 0 订阅

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 

题解:

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

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

代码:

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值