poj_1389 Area of Simple Polygons 线段树+扫描线

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3139 Accepted: 1601

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 

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define FOR(a,b) for(int i =  a ; i < b ; i++)
#define mem0(a) memset(a,0,sizeof(a))
const int N = 50000+10;
struct Node
{
    int x,y1,y2;
    int flag ;
} node[N];
//把一段段平行于y轴的线段表示成数组 ,
//x是线段的x坐标,y1,y2线段对应的下端点和上端点的坐标
//一个矩形 ,左边的那条边f为1,右边的为-1,
//用来记录重叠情况,可以根据这个来计算,nod节点中的c
bool cmp(Node a,Node b)
{
    return a.x  - b.x <0.0000001;
}
struct node
{
    int l,r;//线段树左右的整点
    int ml, mr;//分别对应左右真实的浮点数端点
    int s, len ;//s用来记录重叠情况,len计算实际长度
} a[N*3];
int  y[N];//记录y坐标的数组
void build(int i,int l,int r)
{
    a[i].l = l ;
    a[i].r = r;
    a[i].ml = y[l];
    a[i].mr = y[r];
    a[i].s = 0 ;
    a[i].len = 0;
    if(a[i].l + 1 == a[i].r)
    {
        return ;
    }
    int mid =(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid,r);
}
void callen(int i )
{
    if(a[i].s > 0 )
    {
        a[i].len = a[i].mr -a[i].ml;
    }
    else if(a[i].r - a[i].l == 1)
    {
        a[i].len = 0 ;
    }
    else
    {
        a[i].len = a[i<<1].len+a[i<<1|1].len;
    }
    return ;
}
void update(int i,Node b) //加入线段b,从第一条开始,更新线段树
{
    if(a[i].ml == b.y1 && a[i].mr == b.y2)
    {
        a[i].s += b.flag ;
        callen(i);
        return ;
    }
    if(b.y2 <= a[i<<1].mr )  update(i<<1,b);
    else if(b.y1 >= a[i<<1|1].ml) update(i<<1|1,b);
    else
    {
        Node temp = b;
        temp.y2 = a[i<<1].mr;
        update(i<<1,temp);
        temp =b ;
        temp.y1 = a[i<<1|1].ml;
        update(i<<1|1,temp);
    }
    callen(i);
    return ;
}
int main()
{
    int  x1,y1,x2,y2;
    int t= 1,flag=1;
    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
    {
        if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
        {
            flag = 0;
            sort(node+1,node+t,cmp);
            sort(y+1,y+t);
            build(1,1,t-1);
            update(1,node[1]);//先加入第一条线段
            int  sum = 0 ;
            for(int i = 2 ; i < t ; i++)
            {
                sum += a[1].len * (node[i].x - node[i-1].x);
                update(1,node[i]);//从第二条开始加入
            }
            printf("%d\n",sum);
            t =1 ;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if(x1==-1&&x2==-1&&y1==-1 &&y2==-1)
                break;
        }

            node[t].x= x1;
            node[t].y1 = y1;
            node[t].y2 = y2;
            node[t].flag = 1;//入边
            y[t++] = y1;
            node[t].x = x2;
            node[t].y1 = y1;
            node[t].y2 = y2;
            node[t].flag = -1;
            y[t++] = y2;

    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值