POJ 1389 (discretization + computational geometry)

Source: Internet
Author: User

Area of simple polygons
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3412 Accepted: 1763

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



Test instructions: Rectangular area intersection.
Only the discretization of the useless line tree is also 47MS AC. It's a bit of a lot of space, but opening a vis array into a bool type should be much less space.
///discretization of#include <iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<cmath>using namespacestd;Const intN =2005;structrec{intX1,y1,x2,y2;} Rec[n];intX[n],y[n];intVis[n][n];intk,t;intBinary1 (intvalue) {    intMid,l=0, r=k-1;  while(l<R) {Mid= (l+r) >>1; if(X[mid]==value)returnmid; if(x[mid]<value) L = mid+1; Elser = mid-1; }    returnl;}intBinary2 (intvalue) {    intMid,l=0, r=k-1;  while(l<S) {Mid= (l+r) >>1; if(Y[mid]==value)returnmid; if(y[mid]<value) L = mid+1; Elser = mid-1; }    returnl;}voidinput () {intX1,y1,x2,y2;  while(true) {scanf ("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1==-1&&x2==-1&&y1==-1&&y2==-1) Break; Rec[t].x1= X1,rec[t].y1 = Y1,rec[t].x2=x2,rec[t++].y2 =Y2; X[K]= x1,y[k++] =Y1; X[K]= x2,y[k++] =Y2; } sort (X,x+k); Sort (Y,y+k);}voidsolve () {intT1,t2,t3,t4;  for(intI=0; i<t;i++) {T1=Binary1 (REC[I].X1); T2=Binary1 (REC[I].X2); T3=Binary2 (rec[i].y1); T4=Binary2 (rec[i].y2);  for(intj=t1;j<t2;j++){             for(intL = t3;l<t4;l++) {Vis[j][l]=1; }        }    }    intArea =0;  for(intI=0; i<k;i++){         for(intj=0; j<k;j++) { area+=vis[i][j]* (x[i+1]-x[i]) * (y[j+1]-Y[j]); }} printf ("%d\n", area);}intMain () {intX1,y1,x2,y2;  while(SCANF ("%d%d%d%d", &x1,&y1,&x2,&y2)! =EOF) {        if(x1==-1&&x2==-1&&y1==-1&&y2==-1) Break; memset (Vis,0,sizeof(VIS)); K=0, t=0; Rec[t].x1= X1,rec[t].y1 = Y1,rec[t].x2=x2,rec[t++].y2 =Y2; X[K]= x1,y[k++] =Y1; X[K]= x2,y[k++] =Y2;        Input ();    Solve (); }    return 0;}

POJ 1389 (discretization + computational geometry)

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.