POJ 1389 Area of Simple Polygons(扫描线)

来源:互联网 发布:百度助手 网络不可用 编辑:程序博客网 时间:2024/06/05 10:57
Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536KTotal 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 41 1 5 21 1 2 5-1 -1 -1 -10 0 2 21 1 3 32 2 4 4-1 -1 -1 -1-1 -1 -1 -1  

Sample Output

1810 

Source

Taiwan 2001



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

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

点击打开链接

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#define N 5001using 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
原创粉丝点击