POJ 1389 area of simple polygons (scan line)

Source: Internet
Author: User

Area of simple polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3278 Accepted: 1694

Description

There is n, 1 <= n <= rectangles in the 2-d xy-plane. The four sides of a rectangle is horizontal or vertical line segments. Rectangles is defined by their lower-left and Upper-right corner points. Each corner point was a pair of nonnegative integers in the range of 0 through 50,000 indicating its x and Y coordinate S.

Assume that the contour of their union was 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 is as large as possible. In a 2-d xy-plane, a polygon are defined by a finite set of segments such this every segment extreme (or endpoint) is share D by exactly edges and no subsets of edges have the same property. The segments is edges and their extremes are the vertices of the polygon. A polygon is simple if there are 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 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 is given one by one in a line. In each line for a rectangle, 4 non-negative integers is given. The first is the x and Y coordinates of the lower-left corner. The next is 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

Sample Output

Source

Taiwan 2001


Test instructions: Given the upper-left corner of some rectangles and the lower-right corner, now find the area of these rectangles (some rectangles may overlap, overlapping parts only need to be added once) idea: Maintain the size of the rectangle on the Y axis with the segment tree, then scan from left to right from the x-axis, dividing the effective interval between the two adjacent x into a single rectangle that has the same width, and the length of the rectangle is obtained after the scan, so that the area of the adjacent X's small rectangle is calculated, And so will get all the small rectangles of the effective interval, then their and is the large rectangular overlap is the area.
think carefully about the code and you'll see.
Click to open link

#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)///determines the valid interval of p[i].x-p[i-1].x interval 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)////segment Tree Traversal {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&LT;&LT;1,DOT);    } else if (q[rt<<1|1].lf<=dot.y1) {insert (RT&LT;&LT;1|1,DOT);        } else {tt pt = dot;        Pt.y1 = q[rt<<1|1].lf;        Insert (RT&LT;&LT;1|1,PT);        PT = dot;        Pt.y2 = q[rt<<1].rf;    Insert (RT&LT;&LT;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);        Area of rectangle = long * Width insert (1,p[i]);   } printf ("%d\n", sum); All split small rectangles and} return 0;}




Copyright NOTICE: This article is the original blogger article, if you have special needs, please contact Bo Master qq:793977586.

POJ 1389 area of simple polygons (scan line)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.