POJ 1389 Area of Simple Polygons(面积合并,线段树+离散化)

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3257 Accepted: 1678

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

题意:有很多组测试数据,每组测试数据有多行,每行有4个整数x1,y1, x2,y2 表示一个矩形的最左下顶点和最右上方顶点,以4个-1结束,问给出的所有矩形总面积是多少,被覆盖的面积只加一次。
解题:每个矩形可以能两条线段(平行于X轴的上下两条线段)代替矩形。离散化X坐标点,再用线段树去维护。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define typeH int   //高度的类型
#define MID(l,r) ((l+r)>>1)
const int MAXN = 50005; 

struct Tree
{
    int len,cover; //每个区间范围内的点出现次数>0的个数,每个点覆盖的次数
}root[MAXN*4];
struct RectEdg
{
    typeH H; //每条边距X轴的高度
    int l,r,cnt; //线段的真实左节点与右节点,cnt=1表示矩形的下边,cnt=0则为上边
}edg[MAXN*2];

int W[MAXN*2]; //宽度点的离散化点集

void builde(int l,int r,int k)
{
    root[k].len=root[k].cover=0;
    if(l==r-1)return ;
    int m=MID(l,r);
    builde(l,m,k<<1);
    builde(m,r,k<<1|1);
}
void getlen(int l,int r,int k)
{
    if(root[k].cover>0)
        root[k].len = W[r]-W[l];
    else if(l==r-1)
        root[k].len=0;
    else
        root[k].len = root[k<<1].len + root[k<<1|1].len;
}
void update(int l,int r,int k,int L,int R,int cnt)
{
    if(L<=W[l]&&W[r]<=R)
    {
        root[k].cover += cnt ;
        getlen(l,r,k);
        return ;
    }
    if(l==r-1) return ;

    int m=MID(l,r);
    if(L<=W[m])
        update( l,m,k<<1 , L , R , cnt);
    if(W[m]<R)
        update( m,r,k<<1|1 , L , R , cnt);
    getlen(l,r,k);
}
void getUnique(int& maxlen) //线段树的宽度离散化,去重
{
    int k=maxlen;
    maxlen=1;
    sort(W+1,W+1+k);
    for(int i=2; i<=k; i++)
        if(W[maxlen]!=W[i])
        W[++maxlen]=W[i];
}

bool cmp(RectEdg aa, RectEdg bb)//边高按从小到大排序
{
    return aa.H<bb.H;
}
int maxlen,tot;   //线段树的长度 与 总边数
void addRectEdg(int h1,int h2,int l,int r)
{
    edg[tot].l=l; edg[tot].r=r; edg[tot].H=h1; edg[tot++].cnt=1; //矩形的下边
    edg[tot].l=l; edg[tot].r=r; edg[tot].H=h2; edg[tot++].cnt=-1; //矩形的上边
    W[++maxlen]=l; W[++maxlen]=r;
}

int main()
{
    int  x1,y1,x2,y2 ;
    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)>0)
    {
        if(x1+y1+x2+y2==-4)
            break;
        maxlen=1;
        tot=0;
        if(x1<x2&&y1<y2)
            addRectEdg(y1 , y2 , x1 , x2);
        if(x2>maxlen)maxlen=x2;
        while(1)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1+y1+x2+y2==-4)
            break;
            if(x1<x2&&y1<y2)
            addRectEdg(y1 , y2 , x1 , x2);
            if(x2>maxlen)maxlen=x2;
        }

        sort(edg,edg+tot,cmp);
        getUnique(maxlen);
        builde(1 , maxlen , 1);

        long long area=0;
        for(int i=0; i<tot-1; i++)
        {
            int L,R;
            L=edg[i].l ;
            R=edg[i].r ;
            update(1 , maxlen , 1 , L , R , edg[i].cnt);
            area += (edg[i+1].H-edg[i].H)*root[1].len;

           // printf("%d--%d len=%d , area = %d\n",W[L],W[R],root[1].len,area);
         }
         printf("%lld\n",area);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值