Area of simple polygonstime limit:1000msmemory limit:65536kbthis problem would be judged onPKU. Original id:1389
64-bit integer IO format: %lld Java class name: Main 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.
InputThe 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.OutputFor each test case, output the total area of all simple polygons in a line.
Sample Input
Sample Output
SourceTaiwan 2001 Solving: Line tree scan lines ...
1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 Const intMAXN =50010;6 structNode {7 intlt,rt,cover,sum;8} tree[maxn<<2];9 structLine {Ten intX1,x2,y,up; OneLine (intA =0,intb =0,intc =0,intD =0) { AX1 =A; -x2 =b; -y =C; theup =D; - } - BOOL operator< (ConstLine &t)Const { - if(y = = T.y)returnUp >t.up; + returnY <T.y; - } + } LINE[MAXN]; A voidBuildintLintRintv) { atTree[v].cover = Tree[v].sum =0; -tree[v].lt =L; -Tree[v].rt =R; - if(L +1= = R)return; - intMid = (L + R) >>1; -Build (l,mid,v<<1); inBuild (mid,r,v<<1|1); - } to voidPushup (intv) { + if(tree[v].cover) { -Tree[v].sum = Tree[v].rt-tree[v].lt; the return; *}Else if(tree[v].lt +1==tree[v].rt) { $Tree[v].sum =0;Panax Notoginseng return; - } theTree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; + } A voidUpdateintLtintRtintValintv) { the if(LT <= tree[v].lt && RT >=tree[v].rt) { +Tree[v].cover + =Val; - Pushup (v); $ return; $ } - if(LT < tree[v<<1].RT) Update (lt,rt,val,v<<1); - if(Rt > tree[v<<1|1].lt) Update (lt,rt,val,v<<1|1); the Pushup (v); - }Wuyi intMain () { the intX1,y1,x2,y2; - while(~SCANF ("%d %d%d%d",&x1,&y1,&x2,&y2)) { Wu if(X1 = =-1&& Y1 = =-1&& x2 = =-1&& y2 = =-1) Break; - inttot =0; Aboutline[tot++] = line (X1,x2,y1,1); $line[tot++] = line (x1,x2,y2,-1); - while(~SCANF ("%d %d%d%d",&x1,&y1,&x2,&y2)) { - if(X1 = =-1&& Y1 = =-1&& x2 = =-1&& y2 = =-1) Break; -line[tot++] = line (X1,x2,y1,1); Aline[tot++] = line (x1,x2,y2,-1); + } theSort (line,line+tot); - intRET =0, pre =0; $Build0,50000,1); the for(inti =0; i < tot; ++i) { theRET + = tree[1].sum* (LINE[I].Y-pre); thePre =line[i].y; theUpdate (Line[i].x1,line[i].x2,line[i].up,1); - } inprintf"%d\n", ret); the } the return 0; About}
View Code
POJ 1389 area of simple polygons