Source: http://poj.org/problem? Id = 1151
Give some Rectangles and find the area of these rectangles.
Idea: the question of Line Segment tree + scanning line. Because the data is double, discretization is required.
Code:
# Include <iostream> # include <cstdio> # include <string. h ># include <algorithm> using namespace STD; const int n = 110; struct line {double x1, x2, Y; int flag;} ll [N * 4]; struct rec {double X1, Y1, X2, Y2;} RR [N * 4]; struct tree {int LP, RP, cur; double Len; int getmid () {return (LP + RP)/2 ;}} TT [8 * n]; double XX [4 * n]; double Ep = 1e-6; bool CMP (line, line B) {return. y> B. y;} void built_tree (int lp, int RP, int POS) {TT [POS]. lp = LP; TT [POS]. rp = RP; TT [POS]. cur = 0; TT [POS]. len = 0; If (LP + 1 = RP) return; int mid = TT [POS]. getmid (); built_tree (LP, mid, POS * 2); built_tree (MID, RP, POS * 2 + 1);} void updatelen (int pos) {If (TT [POS]. cur> 0) {TT [POS]. len = XX [TT [POS]. RP-1]-XX [TT [POS]. lp-1];} else {If (TT [POS]. LP + 1 = TT [POS]. RP) TT [POS]. len = 0.0; else TT [POS]. len = TT [POS * 2 + 1]. len + TT [POS * 2]. len ;}} void Update (INT POs, line temp) {If (XX [TT [POS]. lp-1] = temp. x1 & XX [TT [POS]. RP-1] = temp. x2) {TT [POS]. cur + = temp. flag; updatelen (POS); return;} If (TT [POS]. LP + 1 = TT [POS]. RP) return; int mid = TT [POS]. getmid (); If (temp. x2-XX [Mid-1] <= EP) {Update (Pos * 2, temp);} else if (temp. x1-XX [Mid-1]> = EP) Update (Pos * 2 + 1, temp); else {line P = temp; p. x2 = XX [Mid-1]; Update (Pos * 2, P); P = temp; p. x1 = XX [Mid-1]; Update (Pos * 2 + 1, P);} updatelen (POS);} int main () {// freopen ("1.txt ", "r", stdin); int CA = 1, n; while (scanf ("% d", & N) {int CNT = 0; for (INT I = 0; I <n; ++ I) {scanf ("% lf", & RR [I]. x1, & RR [I]. y1, & RR [I]. x2, & RR [I]. y2); LL [CNT]. x1 = RR [I]. x1; ll [CNT]. x2 = RR [I]. x2; ll [CNT]. y = RR [I]. y2; ll [CNT]. flag = 1; XX [CNT] = RR [I]. x1; CNT ++; ll [CNT]. x1 = RR [I]. x1; ll [CNT]. x2 = RR [I]. x2; ll [CNT]. y = RR [I]. y1; ll [CNT]. flag =-1; XX [CNT] = RR [I]. x2; CNT ++;} Sort (LL, LL + CNT, CMP); sort (XX, xx + CNT); int Len = unique (XX, xx + CNT) -xx; built_tree (1, Len, 1); double ans = 0.0; update (1, ll [0]); For (INT I = 1; I <CNT; ++ I) {ans + = (LL [I-1]. y-ll [I]. y) * TT [1]. len; update (1, ll [I]);} printf ("Test Case # % d \ n", CA ++); printf ("total occupied ed area: %. 2lf \ n ", ANS);} return 0 ;}