POJ 1389 area of simple polygons (acreage merge, Segment tree + discretization)

Source: Internet
Author: User

Area of simple polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3257 Accepted: 1678

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: There are many sets of test data, each group of test data has more than one row, each row has 4 integer x1,y1, x2,y2 represents a rectangle's leftmost bottom vertex and the top right vertex, with 4-1 end, asked to give the total area of the rectangle, the covered area is only added once. Problem solving: Each rectangle can be used to replace the rectangle by two segments (parallel to the upper and lower segments of the x-axis). Discretization of the x-coordinate point, and then using the line tree to maintain.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define TYPEH INT// The height of type # define MID (L,r) ((L+R) >>1) const int MAXN = 50005; struct tree{int len,cover;//number of points >0 in each interval range, number of times each point is overwritten}root[maxn*4];struct rectedg{typeh H;//height per margin x axis in T l,r,cnt; The true left and right node of the line segment, Cnt=1 represents the bottom of the rectangle, cnt=0 is the top}edg[maxn*2];int w[maxn*2];    Discretization point set of the width point void builde (int l,int r,int k) {root[k].len=root[k].cover=0;    if (l==r-1) return;    int M=mid (L,R);    Builde (l,m,k<<1); Builde (m,r,k<<1|1);}    void Getlen (int l,int r,int k) {if (root[k].cover>0) Root[k].len = W[r]-w[l];    else if (l==r-1) root[k].len=0; else Root[k].len = Root[k<<1].len + Root[k<<1|1].len;}  void update (int l,int r,int k,int l,int r,int cnt) {if (l<=w[l]&&w[r]<=r) {root[k].cover + = cnt        ;        Getlen (L,R,K);    return;    } if (l==r-1) return;    int M=mid (L,R); if (L<=w[m]) update (L,M,K<<1, L, R, CNT);    if (w[m]<r) update (m,r,k<<1|1, L, R, CNT); Getlen (l,r,k);}    void Getunique (int& maxlen)//Segment tree width discretization, de-weight {int k=maxlen;    Maxlen=1;    Sort (w+1,w+1+k); for (int i=2; i<=k; i++) if (W[maxlen]!=w[i]) w[++maxlen]=w[i];} BOOL CMP (RECTEDG AA, RECTEDG BB)//Edge high Press from small to large to sort {return AA. H<bb. H;}   int Maxlen,tot; The length of the segment tree is the total number of sides void addrectedg (int h1,int h2,int l,int r) {edg[tot].l=l; edg[tot].r=r; Edg[tot]. H=H1; Edg[tot++].cnt=1; The bottom edg[tot].l=l of the rectangle; Edg[tot].r=r; Edg[tot]. H=H2; Edg[tot++].cnt=-1; The upper w[++maxlen]=l of the rectangle; W[++maxlen]=r;}    int main () {int x1,y1,x2,y2;        while (scanf ("%d%d%d%d", &x1,&y1,&x2,&y2) >0) {if (x1+y1+x2+y2==-4) break;        Maxlen=1;        tot=0;        if (x1<x2&&y1<y2) addrectedg (y1, y2, x1, x2);        if (X2>maxlen) maxlen=x2;       while (1) {scanf ("%d%d%d%d", &x1,&y1,&x2,&y2);     if (x1+y1+x2+y2==-4) break;            if (x1<x2&&y1<y2) addrectedg (y1, y2, x1, x2);        if (X2>maxlen) maxlen=x2;        } sort (edg,edg+tot,cmp);        Getunique (MaxLen);        Builde (1, MaxLen, 1);        Long Long area=0;            for (int i=0; i<tot-1; i++) {int l,r;            L=EDG[I].L;            R=EDG[I].R;            Update (1, MaxLen, 1, L, R, edg[i].cnt); Area + = (edg[i+1]. H-edg[i].           H) *root[1].len;         printf ("%d--%d len=%d, area =%d\n", W[l],w[r],root[1].len,area);    } printf ("%lld\n", area); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1389 area of simple polygons (acreage merge, Segment tree + discretization)

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.