POJ1389-Area of Simple Polygons

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3074 Accepted: 1570

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 
//AC代码
/*
题意:求矩形面积的并
此题还是基本和POJ1151一样用线段树+离散化+扫描线
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=20000;//这里尽量开大点,当时就是开小了RE了几次
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
struct Node
{
    int left;
    int right;//线段树的左右整点
    int flag;//记录重叠情况,大于零说明没有重叠
    int cnt;//记录实际的长度
    int lf;//左边端点真实的浮点数
    int rf;//右边端点真是的浮点数
}segTree[Max];
struct Line
{
    int x;
    int y1;
    int y2;
    int ok;
}line[Max];
int y[Max];//记录y坐标的数组
bool cmp(Line u,Line v)//sort排序
{
    return u.x<v.x;
}
void Build_Tree(int t,int left,int right)
{
    segTree[t].left=left;
    segTree[t].right=right;
    segTree[t].lf=y[left];
    segTree[t].rf=y[right];
    if((left+1)==right) return;
    int mid=(left+right)>>1;
    Build_Tree(t<<1,left,mid);
    Build_Tree(t<<1|1,mid,right);//递归构造线段树,这里mid不能+1因为如果+1那么t<<1的右孩子和t<<1|1的左孩子不能产生联系最终更新父节点就是错误的数据
}
void Calen(int t)//计算长度
{
    if(segTree[t].flag>0)//如果没有重叠
    {
        segTree[t].cnt=segTree[t].rf-segTree[t].lf;
    }
    else if(segTree[t].left+1==segTree[t].right)//如果重叠了
    {
        segTree[t].cnt=0;
    }
    else
    {
        segTree[t].cnt=segTree[t<<1].cnt+segTree[t<<1|1].cnt;
    }
}
void Update(int t,Line L)//加入线段L,后更新线段树
{
    if(L.y1==segTree[t].lf&&L.y2==segTree[t].rf)
    {
        segTree[t].flag+=L.ok;
        Calen(t);
    }
    else if(L.y2<=segTree[t<<1].rf)
    {
        Update(t<<1,L);
    }
    else if(L.y1>=segTree[t<<1|1].lf)
    {
        Update(t<<1|1,L);
    }
    else
    {
        Line temp;
        temp=L;
        temp.y2=segTree[t<<1].rf;
        Update(t<<1,temp);
        temp=L;
        temp.y1=segTree[t<<1|1].lf;
        Update(t<<1|1,temp);
    }
    Calen(t);
}
void Init(int &m,int x1,int y1,int x2,int y2)
{
    line[m].x=x1;
    line[m].y1=y1;
    line[m].y2=y2;
    line[m].ok=1;
    y[m]=y1;
    m++;
    line[m].x=x2;
    line[m].y1=y1;
    line[m].y2=y2;
    line[m].ok=-1;
    y[m]=y2;
    m++;
}
int main()
{
    int m,i,j,p;
    int x1,y1,x2,y2,area;
    p=0;
    m=1;
    while(cin>>x1>>y1>>x2>>y2)
    {
        if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
        p+=1;
        else
        p=0;
        if(p==1)
        {
            sort(line+1,line+m,cmp);
            sort(y+1,y+m);
            Build_Tree(1,1,m-1);
            Update(1,line[1]);
            area=0;
            for(i=2;i<m;i++)
            {
                area+=segTree[1].cnt*(line[i].x-line[i-1].x);
                Update(1,line[i]);
            }
            cout<<area<<endl;
            m=1;
        }
        if(p==2)
        break;
        Init(m,x1,y1,x2,y2);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值