Area of Simple Polygons
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 1389
64-bit integer IO format: %lld Java class name: Main
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 4 1 1 5 2 1 1 2 5 -1 -1 -1 -1 0 0 2 2 1 1 3 3 2 2 4 4 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output
18 10
Source
解题:线段树扫描线。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 50010; 6 struct node { 7 int lt,rt,cover,sum; 8 } tree[maxn<<2]; 9 struct Line { 10 int x1,x2,y,up; 11 Line(int a = 0,int b = 0,int c = 0,int d = 0) { 12 x1 = a; 13 x2 = b; 14 y = c; 15 up = d; 16 } 17 bool operator<(const Line &t)const { 18 if(y == t.y) return up > t.up; 19 return y < t.y; 20 } 21 } line[maxn]; 22 void build(int L,int R,int v) { 23 tree[v].cover = tree[v].sum = 0; 24 tree[v].lt = L; 25 tree[v].rt = R; 26 if(L+1 == R) return; 27 int mid = (L + R)>>1; 28 build(L,mid,v<<1); 29 build(mid,R,v<<1|1); 30 } 31 void pushup(int v) { 32 if(tree[v].cover) { 33 tree[v].sum = tree[v].rt - tree[v].lt; 34 return; 35 } else if(tree[v].lt + 1 == tree[v].rt) { 36 tree[v].sum = 0; 37 return; 38 } 39 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; 40 } 41 void update(int lt,int rt,int val,int v) { 42 if(lt <= tree[v].lt && rt >= tree[v].rt) { 43 tree[v].cover += val; 44 pushup(v); 45 return; 46 } 47 if(lt < tree[v<<1].rt) update(lt,rt,val,v<<1); 48 if(rt > tree[v<<1|1].lt) update(lt,rt,val,v<<1|1); 49 pushup(v); 50 } 51 int main() { 52 int x1,y1,x2,y2; 53 while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2)) { 54 if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1) break; 55 int tot = 0; 56 line[tot++] = Line(x1,x2,y1,1); 57 line[tot++] = Line(x1,x2,y2,-1); 58 while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2)) { 59 if(x1 == -1 && y1 == -1 && x2 == -1 && y2 == -1) break; 60 line[tot++] = Line(x1,x2,y1,1); 61 line[tot++] = Line(x1,x2,y2,-1); 62 } 63 sort(line,line+tot); 64 int ret = 0,pre = 0; 65 build(0,50000,1); 66 for(int i = 0; i < tot; ++i) { 67 ret += tree[1].sum*(line[i].y - pre); 68 pre = line[i].y; 69 update(line[i].x1,line[i].x2,line[i].up,1); 70 } 71 printf("%d\n",ret); 72 } 73 return 0; 74 }