HUD 1828 picture rectangular perimeter/line segment tree

Source: Internet
Author: User

Calculate the perimeter of the overlapped rectangle.
Question: Pay attention to the number of line segments projected on the Y axis when enumerating the X range. That is, it corresponds to the number of discontinuous line segments in X, because the width of the rectangle in this relationship. To arrange x, remember that the inbound side is in front and the outbound side is behind. Otherwise, the two rectangles with the intersection of the edges will also include the duplicate side.

# Include <cmath> # include <algorithm> # include <iostream> using namespace STD; # define L (x) (x <1) # define R (X) (x <1 | 1) # define n 20010int y [N * 2]; struct node {int L, R, Len, LC, RC, cover, CNT; /* LC, RC, indicates the specified number of times overwritten */} node [N * 4]; struct line {int X, Y1, Y2, flag ;} line [N * 2]; bool CMP (line A, line B) {if (. x! = B. x) return. x <B. x; return. flag> B. flag;/* When the X phase is at the same time, draw two rectangles for analysis */} void y_update (int u) {If (node [u]. cover> 0) node [u]. len = Y [node [u]. r]-y [node [u]. l]; else if (node [u]. L + 1 = node [u]. r) node [u]. len = 0; else node [u]. len = node [L (u)]. len + node [R (u)]. len; If (node [u]. cover> 0) node [u]. CNT = 1;/* CNT records the number of line segments projected to the Y axis (that is, the number of discontinuous segments) */else {node [u]. CNT = node [L (u)]. CNT + nod E [R (u)]. CNT; If (node [R (u)]. LC! = 0 & node [L (u)]. RC! = 0)/* If the right endpoint of the Left subtree is overwritten with the left endpoint of the right subtree, it indicates that there is an interval spanning the left and right subtree in the entire interval, but it is duplicated, so we need to subtract 1 */node [u]. CNT -- ;}} void build (int u, int L, int R) {node [u]. L = L; node [u]. R = r; node [u]. CNT = 0; node [u]. len = node [u]. cover = 0; node [u]. lc = node [u]. rc = 0; If (L + 1 = r) return; int mid = (L + r)> 1; build (L (u), L, mid ); build (R (u), mid, R);} void Update (int u, line E) {If (E. y1 = Y [node [u]. l]) node [u]. LC + = E. flag; If (E. y2 = Y [node [u]. r]) node [u]. RC + = E. flag; If (E. y1 = Y [node [u]. l] & E. y2 = Y [node [u]. r]) {node [u]. cover + = E. flag; y_update (U); return;} If (E. y1> = Y [node [R (u)]. l]) Update (R (u), e); else if (E. y2 <= Y [node [L (u)]. r]) Update (L (u), e); else {Line temp1 = E; line temp2 = E; temp1.y2 = Y [node [L (u)]. r]; temp2.y1 = Y [node [R (u)]. l]; Update (L (u), temp1); Update (R (u), temp2);} y_update (U);} int main () {// freopen ("a.txt", "r", stdin); int N, I, k, X1, Y1, X2, Y2; while (scanf ("% d", & N )! = EOF) {for (I = k = 1; I <= N; I ++, K ++) {scanf ("% d ", & X1, & Y1, & X2, & Y2); line [K]. X = x1; line [K]. y1 = Y1; line [K]. y2 = Y2; line [K]. flag = 1; y [k] = Y1; k ++; line [K]. X = x2; line [K]. y1 = Y1; line [K]. y2 = Y2; line [K]. flag =-1; y [k] = Y2;} Sort (LINE + 1, line + k, CMP); sort (Y + 1, Y + k ); build (1, 1, k-1); Update (1, line [1]); int ans = node [1]. len; int temp = node [1]. len; int tcnt = node [1]. CNT; for (I = 2; I <K; I ++) {update (1, line [I]); ans + = ABS (node [1]. len-Temp); ans + = 2 * (line [I]. x-line [I-1]. x) * tcnt; temp = node [1]. len; tcnt = node [1]. CNT;} printf ("% d \ 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.