HDU 1542 Atlantic TIs (line segment tree scanning line)

Source: Internet
Author: User

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): 6788 accepted submission (s): 2970


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

Question:

N rectangles are given, and the edges are parallel to the coordinate axes. Ask the area of the final graph.

Analysis:

Scanned line of the Line Segment tree.

Because the coordinates are floating-point numbers and a little big, discretization is required first. I use a line parallel to the X axis to scan from the bottom up, so I only need to discretization the X coordinates; A few days ago, I learned a good method of discretization for a certain question: Using sort () & unique (), I used map (Red/black tree );

How to scan the scanning line? First, the rectangle is divided into the bottom (1) and top (-1), and the record height (Y) is arranged in ascending order of height; then start scanning. When the corresponding line segment below is + 1, the top is-1, so that the range greater than 0 corresponds to the cable segment, and the corresponding area is calculated for each scan, and the sum can be obtained.

The advantages of maintaining the left-closed and right-open intervals are apparent ....



Add the first line



For example, calculate the blue area



For example, add the second line.



For example, calculate the green area.



Add line 3



For example, calculate the purple area.



For example, add the last line (that's all)


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x )) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm # define maxn 202 using namespace STD; struct edge {double XL, xr, y; int V; void _ set (double A, double B, double H, double Val) {XL = A; xr = B; y = H; V = val ;} bool operator <(const edge & E) const {return Y <E. Y ;}} edge [202]; Double X [maxn]; double sum [maxn <2]; int Val [maxn <2]; inline void pushup (int K, int L, int R) {If (Val [k]) sum [k] = x [R]-X [l]; else if (R-l = 1) sum [k] = 0; else sum [k] = sum [K * 2 + 1] + sum [K * 2 + 2];} void Update (int, int B, int V, int K, int L, int R) {If (B <= L | r <= A) return; if (A <= L & R <= B) {Val [k] + = V; pushup (K, L, R) ;} else {update (A, B, v, K * 2 + 1, l, l + r> 1); Update (a, B, V, K * 2 + 2, L + r> 1, r); pushup (K, L, R) ;}} int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T ", "r", stdin) ;#endif // online_judge int N; double x1, x2, Y1, Y2; int M = 202; int t_t = 0; while (scanf ("% d", & N), n) {for (INT I = 0; I <n; I ++) {scanf ("% lf", & X1, & Y1, & X2, & Y2); edge [I]. _ set (x1, x2, Y1, 1); edge [I + N]. _ set (x1, x2, Y2,-1); X [I] = x1; X [I + N] = x2;} n <= 1; sort (edge, edge + n); sort (x, x + n); int xn = unique (x, x + n)-X; double area = 0; For (INT I = 0; I <n; I ++) {if (I) area + = (edge [I]. y-edge [I-1]. y) * sum [0]; int A = lower_bound (x, x + Xn, edge [I]. XL)-X; int B = lower_bound (x, x + Xn, edge [I]. xr)-X; int v = edge [I]. v; Update (a, B, V, 0, 0, m);} t_t ++; printf ("Test Case # % d \ n", t_t ); printf ("total received area: %. 2f \ n ", area);} 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.