Poj 1177, poj1177
Question Link
Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. their sides are all vertical or horizontal. each rectangle can be partially or totally covered by the others. the length of the boundary of the union of all rectangles is called the perimeter.
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.
The corresponding boundary is the whole set of line segments drawn in Figure 2.
The vertices of all rectangles have integer coordinates.
Input
Your program is to read from standard input. the first line contains the number of rectangles pasted on the wall. in each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. the values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.
0 <= number of rectangles <5000
All coordinates are in the range [-keys, 10000] and any existing rectangle has a positive area.
Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
Sample Input
7-15 0 5 10-5 8 20 2515 -4 24 140 -6 16 42 15 10 2230 10 36 2034 0 40 16
Sample Output
228
Source
IOI 1998: there are multiple rectangles. The two sides of the rectangle are parallel to the coordinate axis. These rectangles may overwrite each other to find the perimeter. Idea: record the two vertical edges (x1, y1, y2) and (x2, y1, y2) of each rectangle, and sort all the vertical edges in ascending order of x, then, when a vertical edge starts to calculate the perimeter, the line perpendicular to the X axis where the vertical side is located is the scanning line. Each time we move to a new vertical edge, we need to calculate the useful edge length of the vertical side scanning line (that is, the useful part of the current vertical side, which may be covered by the current vertical side ), and the length of the horizontal side * Number of horizontal lines before the current scanning line and the previous scanning line, until the last vertical side is calculated, that is, the complete perimeter. The Code is as follows:
1 # include <iostream> 2 # include <algorithm> 3 # include <cstdio> 4 # include <cstring> 5 # include <vector> 6 using namespace std; 7 const int N = 5005; 8 struct Line {9 int x, y1, y2; 10 int flag; 11 bool operator <(const Line s) 12 {13 if (x = s. x) return flag> s. flag; 14 return x <s. x; 15} 16} line [N * 2]; 17 18 struct Tree 19 {20 int l, r; 21 bool lf, rf; // whether the left and right boundary points are covered; 22 int cover_len; 23 int cover_num; 24 int num; // Number of rectangles; 25} tr [N * 10]; 26 27 vector <int> v; 28 29 void build (int l, int r, int I) 30 {31 tr [I]. l = l; tr [I]. r = r; 32 tr [I]. cover_len = 0; 33 tr [I]. cover_num = 0; 34 tr [I]. num = 0; 35 tr [I]. lf = tr [I]. rf = false; 36 if (l + 1 = r) return; 37 int mid = (l + r)> 1; 38 build (l, mid, I <1); 39 build (mid, r, I <1 | 1); 40} 41 void process (int I) 42 {43 if (tr [I]. cover_num> 0) 44 {45 tr [I]. cover_len = v [tr [I]. r]-v [tr [I]. L]; 46 tr [I]. lf = tr [I]. rf = true; 47 tr [I]. num = 1; 48 return; 49} 50 if (tr [I]. l + 1 = tr [I]. r) 51 {52 tr [I]. cover_len = 0; 53 tr [I]. num = 0; 54 tr [I]. lf = tr [I]. rf = false; 55 return; 56} 57 int ls = (I <1); 58 int rs = (I <1 | 1); 59 tr [I]. cover_len = tr [ls]. cover_len + tr [rs]. cover_len; 60 tr [I]. num = tr [ls]. num + tr [rs]. num-(tr [ls]. rf & tr [rs]. lf); 61 tr [I]. lf = tr [ls]. lf; tr [I]. rf = tr [rs]. rf; 62} 63 void update (int I, Line t) 64 {65 if (t. y1 <= v [tr [I]. l] & v [tr [I]. r] <= t. y2) 66 {67 tr [I]. cover_num + = t. flag; 68 process (I); 69 return; 70} 71 int mid = (tr [I]. l + tr [I]. r)> 1; 72 if (t. y1 <v [mid]) update (I <1, t); 73 if (t. y2> v [mid]) update (I <1 | 1, t); 74 process (I); 75} 76 int main () 77 {78 int n; 79 while (scanf ("% d", & n )! = EOF) 80 {81 v. clear (); 82 for (int I = 0; I <n; I ++) 83 {84 int x1, y1, x2, y2; 85 scanf ("% d", & x1, & y1, & x2, & y2); 86 line [I * 2]. x = x1; line [I * 2]. y1 = y1; line [I * 2]. y2 = y2; line [I * 2]. flag = 1; 87 line [I * 2 + 1]. x = x2; line [I * 2 + 1]. y1 = y1; line [I * 2 + 1]. y2 = y2; line [I * 2 + 1]. flag =-1; 88 v. push_back (y1); 89 v. push_back (y2); 90} 91 sort (line, line + 2 * n); 92 sort (v. begin (), v. end (); 93 int num = unique (v. begin (), v. end ()-v. begin (); 95 build (0, num-1, 1); 96 int ans = 0, len = 0; 97 for (int I = 0; I <2 * n; I ++) 98 {99 if (I> 0) ans + = tr [1]. num * 2 * (line [I]. x-line [I-1]. x); 100 update (1, line [I]); 101 ans + = abs (tr [1]. cover_len-len); 102 len = tr [1]. cover_len; 103} 104 printf ("% d \ n", ans); 105} 106 return 0; 107}