POJ 1389 Area of Simple Polygons(扫描线)

Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3278 Accepted: 1694

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




    题意:给出一些矩形的左上角与右下角,现在求这些矩形的面积(有些矩形可能会重叠,重叠部分只需相加一次就可以了)
    思路:用线段树维护矩形在y轴上的大小,然后从x轴自左向右进行扫描,将两个相邻的x间的有效区间分成一个一个的矩形,这些矩形拥有一样的宽,扫描之后会得到这些矩形的长的和,这样相邻的x的小矩形的面积就求出了,以此类推会得到所有的有效区间的小矩形,则他们的和就是大矩形进行重叠是的面积。

    仔细思考一下代码就可以明白了。


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define N 5001

using namespace std;

struct node
{
    int l,r,ans;
    int lf,rf,cnt;
} q[N<<2];

struct tt
{
    int x,y1,y2;
    int flag;
} p[N<<2];

int pnum[N<<2];

int k;

bool cmp(tt a,tt b)
{
    return a.x<b.x;
}

void build(int l,int r,int rt)
{
    q[rt].l = l;
    q[rt].r = r;
    q[rt].ans = 0;
    q[rt].lf = pnum[l];
    q[rt].rf = pnum[r];
    q[rt].cnt = 0;
    if(l+1 == r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,rt<<1);
    build(mid,r,rt<<1|1);
}

void updata(int rt)   ///确定p[i].x - p[i-1].x区间y的有效区间
{
    if(q[rt].ans>0)
    {
        q[rt].cnt = q[rt].rf - q[rt].lf;   
        return ;
    }
    if(q[rt].l + 1 == q[rt].r)
    {
        q[rt].cnt = 0;
    }
    else
    {
        q[rt].cnt = q[rt<<1].cnt + q[rt<<1|1].cnt;
    }
}

void insert(int rt,tt dot)   /// 线段树进行遍历
{
    if(q[rt].lf == dot.y1 && q[rt].rf == dot.y2)
    {
        q[rt].ans += dot.flag;
        updata(rt);
        return ;
    }
    if(q[rt<<1].rf >= dot.y2)  
    {
        insert(rt<<1,dot);
    }
    else if(q[rt<<1|1].lf<=dot.y1)
    {
        insert(rt<<1|1,dot);
    }
    else
    {
        tt pt = dot;
        pt.y1 = q[rt<<1|1].lf;
        insert(rt<<1|1,pt);
        pt = dot;
        pt.y2 = q[rt<<1].rf;
        insert(rt<<1,pt);
    }
    updata(rt);
}

int main()
{
    int a,b,c,d;
    while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
    {
        k = 1;
        if(a == -1 && b == -1 && c == -1 && d == -1)
        {
            break;
        }
        p[k].x = a;
        p[k].y1 = b;
        p[k].y2 = d;
        p[k].flag = 1;
        pnum[k] = b;
        k++;
        p[k].x = c;
        p[k].y1 = b;
        p[k].y2 = d;
        p[k].flag = -1;
        pnum[k] = d;
        k++;
        int pf = 1;
        while(pf)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(a == -1 && b == -1 && c == -1 && d == -1)
            {
                pf = 0;
                break;
            }
            p[k].x = a;
            p[k].y1 = b;
            p[k].y2 = d;
            p[k].flag = 1;
            pnum[k] = b;
            k++;
            p[k].x = c;
            p[k].y1 = b;
            p[k].y2 = d;
            p[k].flag = -1;
            pnum[k] = d;
            k++;
        }
        k--;
        sort(p+1,p+k+1,cmp);
        sort(pnum+1,pnum+k+1);
        build(1,k,1);
        insert(1,p[1]);
        int sum = 0;
        for(int i=2;i<=k;i++)
        {
            sum += q[1].cnt * (p[i].x - p[i-1].x);   /// 矩形的面积 = 长 * 宽
            insert(1,p[i]);
        }
        printf("%d\n",sum);   ///所有分割的小矩形的和
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值