Poj1151 Atlanta [scanning line] + [Line Segment tree] + [discretization]

Source: Internet
Author: User

Atlanta
Time limit:1000 ms   Memory limit:10000 K
Total submissions:16882   Accepted:6435

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 

I am tired ~~ This problem was solved at a.m. and ended at a.m.. I didn't understand the scanning line in the morning. Then I saw some leaders in the evening. I was very unfamiliar with the results, it was already midnight when I finished writing all the pieces of Suo, And I submitted various RESS with joy. I started to think it was a small array, and then I doubled it again, in this way, after Re 6 or 7 times, I suddenly found that freopen forgot to comment out, O (begin □commit) o... nima !! The problem is always found before going to bed. Given the diagonal coordinates of N rectangles, they are bottom left and top right, floating point, respectively, to find the area covered by the rectangle. Question: The scanning line solution separates each rectangle in a straight line based on its length and width. In the end, there are 2n cut lines perpendicular to the X axis and 2n cut lines perpendicular to the Y axis, store these cut lines in two arrays and sort them. Create a line segment tree to maintain the length of the scanning line that is currently covered in the Y axis. Finally, when scanning the line from left to right, after a cut line perpendicular to the X axis, the cut line is immediately updated to the line segment tree. The coverage area is the distance between the two adjacent scanning lines multiplied by the length of the Left scanning line, the area to be covered is accumulated. The X-axis cutover array needs to record whether the edge is an inbound or outbound edge. If the outbound edge is an outbound edge, the area on the right is excluded. However, when the inbound edge overlaps, you need to consider it in detail.

# Include <stdio. h >#include <algorithm> # define maxn 202 # define lson L, mid, RT <1 # define rson mid, R, RT <1 | 1 Using STD :: sort; struct node {double Y1, Y2, height; // Y1, Y2 records int covertimes before y coordinate discretization;} tree [maxn <2]; // Interval Tree double yarr [maxn]; // cutover line perpendicular to the Y axis struct node {Double X, Y1, Y2; int isleftedge;} xarr [maxn]; // bool CMP (node A, Node B) {return. x <B. x;} void build (int l, int R, int RT) {tree [RT]. covertimes = 0; tree [RT]. height = 0; tree [RT]. y1 = yarr [l]; tree [RT]. y2 = yarr [R]; If (R-l = 1) return; int mid = (L + r)> 1; build (lson ); build (rson);} void getsweeplinesheight (int l, int R, int RT) {// due to line segment coverage, therefore, the end of the long line segment does not indicate that the scanning line length is 0if (tree [RT]. covertimes> 0) {tree [RT]. height = tree [RT]. y2-tree [RT]. y1;} else if (R-l = 1) {tree [RT]. height = 0;} else tree [RT]. height = tree [RT <1]. height + tree [RT <1 | 1]. height;} void Update (node xnode, int L, int R, int RT) {If (xnode. y1 = tree [RT]. y1 & xnode. y2 = tree [RT]. y2) {tree [RT]. covertimes + = xnode. isleftedge; getsweeplinesheight (L, R, RT); return;} // include R-l = 1int mid = (L + r)> 1; if (xnode. y2 <= yarr [Mid]) Update (xnode, lson); else if (xnode. y1> = yarr [Mid]) Update (xnode, rson); else {node temp = xnode; temp. y2 = yarr [Mid]; Update (temp, LSO N); temp = xnode; temp. Y1 = yarr [Mid]; Update (temp, rson);} getsweeplinesheight (L, R, RT); // attention !} Int main () {// freopen ("stdin.txt", "r", stdin); int N, I, CAS = 1, ID; double X1, Y1, X2, Y2, SUM; while (scanf ("% d", & N), n) {for (I = id = 0; I <n; ++ I) {scanf ("% lf", & X1, & Y1, & X2, & Y2); yarr [ID] = Y1; xarr [ID]. X = x1; xarr [ID]. isleftedge = 1; // 1 indicates left,-1 indicates right xarr [ID]. y1 = Y1; xarr [ID ++]. y2 = Y2; yarr [ID] = Y2; xarr [ID]. X = x2; xarr [ID]. isleftedge =-1; xarr [ID]. y1 = Y1; xarr [ID ++]. y2 = Y2;} Sort (yarr, yarr + id); sort (xarr, xarr + id, CMP); Build (0, ID-1, 1 ); update (xarr [0], 0, ID-1, 1); for (I = 1, sum = 0; I <ID; ++ I) {sum + = tree [1]. height * (xarr [I]. x-xarr [I-1]. x); Update (xarr [I], 0, ID-1, 1);} printf ("Test Case # % d \ ntotal received area: %. 2lf \ n ", CAS ++, sum);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.