HDU 1542 Atlantis (line segment Tree & scanning line & Area merge)

Source: Internet
Author: User
Atlanta Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6386 accepted submission (s): 2814


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 #1Total explored area: 180.00 
 
Sourcemid-Central European Regional Contest 2000
Recommendlinle | we have carefully selected several similar problems for you: 1828 1255 1823 1543 question: give you some rectangles parallel to the coordinate axes. Tell you the coordinates in the lower left corner and upper right corner. Ask you now. The area covered by the entire coordinate plane. Idea: the question of scanning line of the Line Segment tree. So I didn't think about other ideas. Let's explain why we can use a scanning line. The first is the area solution. Area = bottom side length * height. The bottom edge length is not necessarily consecutive. So if we know the bottom edge length and height at a certain time point, we can calculate the area. The line segment tree can be used to find the bottom edge length of a certain time (the length of the X axis overwritten ). Therefore, we only need to sort all parallel and X-axis edges by height. Insert them to the line segment tree in sequence. Add the bottom of the rectangle to the line segment tree. When it comes to the above, the corresponding bottom is deleted from the line segment tree. The text may not be easy to understand. Just look at the drawing. For details, see the code:
# Include <algorithm> # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 250; # define lson L, mid, ls # define rson Mid + 1, R, rsint n, m; int cov [maxn <2]; double Len [maxn <2], H [maxn]; struct node {double x1, x2, H; int V; node (double A = 0, double B = 0, double C = 0, int D = 0): x1 (A), X2 (B), H (c ), V (d) {}} seg [maxn]; bool CMP (node A, Node B) {return. h <B. h;} void init () {Sort (H, H + M); M = unique (H, H + M)-H;} int Hash (Double X) {return lower_bound (H, H + M, x)-H;} void pushup (int l, int R, int RT) {If (COV [RT]) // The entire block must have covered Len [RT] = H [R + 1]-H [l]; else if (L = r) // no more left and right sons. Len [RT] = 0; else // The entire block is not covered but partially covered by Len [RT] = Len [RT <1] + Len [RT <1 | 1];} void Update (int l, int R, int RT, int L, int R, int d) {If (L <= L & R <= r) {cov [RT] + = D; pushup (L, R, RT); Return ;}// this flag does not need to be passed down. Because the above one is not deleted. There must be a bottom line. Int mid = (L + r)> 1, ls = RT <1, RS = ls | 1; if (L <= mid) Update (lson, L, R, d); If (r> mid) Update (rson, L, R, D); pushup (L, R, RT);} int main () {int CAS = 1, i, PTR; double x1, x2, Y1, Y2, ans; while (scanf ("% d", & N), n) {printf ("Test Case # % d \ n", CAS ++); for (I = PTR = 0; I <n; I ++) {scanf ("% lf", & X1, & Y1, & X2, & Y2); H [PTR] = x1; SEG [PTR ++] = node (x1, x2, Y1, 1); H [PTR] = x2; seg [PTR ++] = node (x1, x2, Y2, -1);} M = PTR, ANS = 0; Init (); sort (SEG, SEG + PTR, CMP); memset (Len, 0, sizeof Len ); memset (COV, 0, sizeof COV); for (I = 0, PTR --; I <PTR; I ++) {Update (0 m-1, 1, hash (SEG [I]. x1), hash (SEG [I]. x2)-1, SEG [I]. v); // M knots. ans + = (SEG [I + 1]. h-seg [I]. h) * Len [1];} printf ("total occupied ed area: %. 2lf \ n ", ANS);} 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.