HDU 1255 covered area (scanned line)

Source: Internet
Author: User

There are T groups of test data. Each group of data is given a number n first. In the next n rows, four floating points in each row indicate the coordinates in the upper left corner and the lower right corner of the rectangle, it is required that these rectangles cover at least two areas.

The difference with poj 1151 Atlanta is that what we add in the answer is not to overwrite the length once multiplied by the X coordinate difference between the two lines, but to overwrite the length twice, the problem is that if the area is covered twice from the covered area.

For the convenience of the description, we assume Len [2] represents the length of the current line segment that is overwritten twice, and Len [1] represents the length of the current line segment that is overwritten once, len [0] is the length of this line segment, and can meet Len [2] + Len [1] = Len [0].

First, if the current line segment has been overwritten twice, the Len [2] of this line segment should be equal to Len [0], and Len [1] should be equal to 0.

Second, if the current line segment is overwritten once, then the Len [2] of this line segment should be, len [2] of the left and right Child Line Segments and Len [1] of the left and right Child Line Segments. Of course, the premise is that the current line segment cannot be the leaf node in the line segment tree, otherwise, it will have no left or right child lines, right? At this time, the Len [2] of the current line segment should be equal to 0. Len [1] is equal to Len [0]. Note that Len [1] of the current line segment must be subtracted from Len [2]. to meet Len [1] + Len [2] = Len [0].

Finally, if this line segment has not been overwritten and the current line segment is not the leaf node in the line segment tree, then its Len [1] and Len [2] should be obtained from Len [1] and Len [2] of its left and right Child Line Segments. If it is a leaf node, then Len [1] and Len [2] are both equal to 0.

# Include <iostream> # include <cstdio> # include <cstring> # include <vector> # include <map> # include <algorithm> using namespace STD; # define LL (X) (x <1) # define RR (x) (x <1 | 1) # define mid (a, B) (a + (B-a)> 1) const int n = 2000; struct line {int flag; Double X, Y1, Y2; line () {} line (double A, double B, double C, int d) {x = A; Y1 = B; y2 = C; flag = D;} bool operator <(const line & B) const {return x <B. X ;}}; struct node {int LFT, rht, FL AG; double Len [3]; int mid () {return mid (LFT, rht);} void Init () {memset (Len, 0, sizeof (LEN) ;}}; int N; vector <double> Y; vector <line> line; Map <double, int> h; struct segtree {node tree [N * 4]; void calu_len (int ind) {If (tree [ind]. flag> = 2) {tree [ind]. len [2] = tree [ind]. len [0]; tree [ind]. len [1] = 0;} else if (tree [ind]. flag = 1) {If (tree [ind]. LFT + 1 = tree [ind]. RHT) tree [ind]. len [2] = 0; else tree [ind]. len [2] = tree [LL (IND)]. len [2] + Tree [RR (IND)]. len [2] + tree [LL (IND)]. len [1] + tree [RR (IND)]. len [1]; tree [ind]. len [1] = tree [ind]. len [0]; tree [ind]. len [1]-= tree [ind]. len [2];} else {If (tree [ind]. LFT + 1 = tree [ind]. RHT) tree [ind]. len [1] = tree [ind]. len [2] = 0; else {tree [ind]. len [2] = tree [LL (IND)]. len [2] + tree [RR (IND)]. len [2]; tree [ind]. len [1] = tree [LL (IND)]. len [1] + tree [RR (IND)]. len [1] ;}}void build (int lft, int rht, int IND) {tree [ind]. LFT = LFT; tree [ind]. Rht = rht; tree [ind]. init (); tree [ind]. flag = 0; tree [ind]. len [0] = Y [rht]-y [LFT]; If (LFT + 1! = RHT) {int mid = tree [ind]. mid (); Build (LFT, mid, LL (IND); Build (MID, rht, RR (IND) ;}} void updata (INT St, int Ed, int ind, int valu) {int LFT = tree [ind]. LFT, rht = tree [ind]. rht; If (ST <= LFT & rht <= ed) tree [ind]. flag + = valu; else {int mid = tree [ind]. mid (); If (ST <mid) updata (St, Ed, LL (IND), valu); If (Ed> mid) updata (St, Ed, rr (IND), valu) ;}calu_len (IND) ;}} seg; int main () {int t; scanf ("% d", & T ); while (t --) {Y. clear (); H. clear (); line. c Lear (); scanf ("% d", & N); double X1, Y1, X2, Y2; For (INT I = 0; I <n; I ++) {scanf ("% lf", & X1, & Y1, & X2, & Y2); line. push_back (line (x1, Y1, Y2, 1); line. push_back (line (X2, Y1, Y2,-1); Y. push_back (Y1); Y. push_back (Y2);} Sort (line. begin (), line. end (); sort (Y. begin (), Y. end (); Y. erase (unique (Y. begin (), Y. end (), Y. end (); For (INT I = 0; I <(INT) Y. size (); I ++) H [Y [I] = I; seg. build (0, (INT) Y. size ()-1, 1); double res = 0; For (INT I = 0; I <( INT) line. Size (); I ++) {if (I! = 0) RES + = (line [I]. x-line [I-1]. x) * seg. tree [1]. len [2]; seg. updata (H [line [I]. y1], H [line [I]. y2], 1, line [I]. flag);} printf ("%. 2lf \ n ", Res);} 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.