Poj 1151 Atlantic (line segment tree + scanning line + discretization), pojatlantic
Atlanta
Time Limit:1000 MS |
|
Memory Limit:10000 K |
Total Submissions:18061 |
|
Accepted:6873 |
Description
There are several except 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.
Input
The input 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.
Output
For 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 #1Total explored area: 180.00
Calculates the area covered by the rectangle. You can delete all line segments of the rectangle parallel Y axis, so that there are many lines of the parallel X axis. Sort the y values of a line segment from small to large. Insert a line segment each time to obtain the length currently covered in the X axis. Multiply it by the z coordinate difference between the line segment and the next line segment, that is, the area. Sum. See
# Include <stdio. h> # include <math. h> # include <string. h> # include <stdlib. h >#include <vector >#include <queue> # include <algorithm> using namespace std; # define N 410 # define ll _ int64double x [N]; struct line // record the line segment information that is parallel to the X axis {int f; // if f is 0, 1 indicates the bottom side or the top side double x1, x2, y; // The left-to-right endpoint of a line segment on the Y axis} a [N]; struct node // The Line Segment Tree Structure {int f; // record whether the interval overlaps the Left and Right endpoints of the double l, r, len; // record interval, and actual overwrite length} f [N * 3]; bool cmp (line, line B) {return. y <B. y;} void creat (int t, int l, int r) {f [t]. l = x [l]; f [t]. r = x [r]; f [t]. len = f [t]. f = 0; if (l = R-1) return; int tmp = t <1, mid = (l + r)> 1; creat (tmp, l, mid); creat (tmp | 1, mid, r); // The line segment is continuous and the length is f [t]. r-f [t]. l} void pushup (int t) {if (f [t]. f) f [t]. len = f [t]. r-f [t]. l; else f [t]. len = f [t <1]. len + f [t <1 | 1]. len;} void update (int t, line a) {if (f [t]. l =. x1 & f [t]. r =. x2) {f [t]. f + =. f; pushup (t); return;} int tmp = t <1; if (. x2 <= f [tmp]. r) // The line segment falls on the left subinterval update (tmp, a); else if (. x1> = f [tmp | 1]. l) /// the line segment falls on the right subinterval update (tmp | 1, a); The else // line segment falls on the left subinterval {line B =; // split the line segment into two segments that belong to two segments B. x2 = f [tmp]. r; update (tmp, B); B = a; B. x1 = f [tmp]. r; update (tmp | 1, B);} pushup (t); // sum to the parent node} int main () {int I, n, cnt = 1, m; double x1, x2, y1, y2; while (scanf ("% d", & n), n) {for (I = m = 1; I <= n; I ++) {scanf ("% lf", & x1, & y1, & x2, & y2); a [m]. x1 = x1; a [m]. x2 = x2; a [m]. y = y1; a [m]. f = 1; x [m ++] = x1; a [m]. x1 = x1; a [m]. x2 = x2; a [m]. y = y2; a [m]. f =-1; x [m ++] = x2;} sort (x + 1, x + m); // sort (a + 1, a + m, cmp); creat (m-1); update (1, a [1]); double ans = 0; for (I = 2; I <m; I ++) {ans + = f [1]. len * (a [I]. y-a [I-1]. y); update (1, a [I]);} printf ("Test case # % d \ n", cnt ++); printf ("Total occupied ed area: %. 2f \ n ", ans);} return 0 ;}