La la ~ Continue Learning Algorithms
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1542
Atlanta
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 7349 accepted submission (s): 3231
Problem descriptionthere are several generated ent Greek texts that contain descriptions of the fabled island Atlanta. some of these texts even include maps of parts of the island. but unfortunately, these maps describe different regions of Atlanta. your friend Bill has to know the total area for which maps exist. you (unwisely) volunteered to write a program that calculates this quantity.
Inputthe input file consists of several test cases. each test case starts with a line containing a single integer N (1 <= n <= 100) of available maps. the N following lines describe one map each. each of these lines contains four numbers x1; Y1; x2; Y2 (0 <= X1 <X2 <= 100000; 0 <= Y1 <Y2 <= 100000), not necessarily integers. the values (x1; Y1) and (X2; Y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don't process it.
Outputfor each test case, your program shocould output one section. the first line of each section must be "Test Case # K", where k is the number of the test case (starting with 1 ). the second one must be "total occupied ed area: a", where A is the total occupied ed area (I. e. the area of the Union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Sample Input
210 10 20 2015 15 25 25.50
Sample output
Test Case #1 total occupied ed area: 180.00 reference from: http://blog.csdn.net/kk303/article/details/9493265 (1) Pay attention to double ~ (2) solve the discretization problem with binary# Include <iostream> # include <string. h >#include <cstdio >#include <algorithm> const int n = 220; const double EPS = 1e-6; using namespace STD; struct node {double x1, x2, Y; int TP;} line [N * 4]; bool CMP (node A, Node B) {return. y-b.y <EPS; // or write the return. y-b.y <EPS precision problem} int N, times [N * 4]; double sum [N * 4]; // implicitly constructed line segment tree double XX [N * 4]; int find (Double X) {int L = 1, R = N; while (L <= r) {int mid = (L + r)/2; If (XX [Mid] = x) return mid; else if (XX [Mid] <X) L = Mid + 1; else r = mid;} return l ;} void Update (int x, int T, int L, int R, int v) {If (L = r) {Times [x] + = T; if (Times [x]) sum [v] = XX [x + 1]-XX [X]; else sum [v] = 0; return ;} int mid = (L + r)/2; If (x <= mid) Update (x, t, L, mid, 2 * V); else Update (x, t, mid + 1, R, 2 * V + 1); sum [v] = sum [2 * V] + sum [2 * V + 1];} int main () {int test = 1; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; memset (sum, 0, sizeof (SUM); memset (times, 0, sizeof (times )); memset (XX, 0, sizeof (XX); int num = 0; For (INT I = 1; I <= N; I ++) {double X1, Y1, x2, Y2; scanf ("% lf", & X1, & Y1, & X2, & Y2); line [++ num]. x1 = x1; line [num]. x2 = x2; line [num]. y = Y1; line [num]. TP = 1; XX [num] = x1; line [++ num]. x1 = x1; line [num]. x2 = x2; line [num]. y = Y2; line [num]. TP =-1; XX [num] = x2;} n = N * 2; sort (xx + 1, xx + 1 + num); sort (LINE + 1, line + 1 + num, CMP); double ans = 0.0; For (INT I = 1; I <= num; I ++) {ans + = sum [1] * (line [I]. y-line [I-1]. y); int L = find (line [I]. x1); int r = find (line [I]. x2)-1; for (Int J = L; j <= r; j ++) Update (J, line [I]. TP, 1, n, 1);} printf ("Test Case # % d \ n", test ++); printf ("total occupied ed area: %. 2lf \ n ", ANS); printf (" \ n ");} return 0 ;}
HDU 1542 line segment tree + scanning line