The points on the x-axis are discretized, and the scan lines are scanned upward along the y-axis
Each time an edge is added, the length of the active edge of the current state is constantly found, and the area of a valid rectangle is obtained based on the height difference between the length and the next edge.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 6 using namespacestd;7 8 Const intmax= $+Ten;9 intflag[max<<2];//record the bottom edge number of an intervalTen Doublesum[max<<2];//record the bottom edge total length of an interval One DoubleX[max];//discretization of X, otherwise x is a floating-point number and is too large to segment the tree A - structseg{//segment - Doublex1,x2,y; the intD; - Seg () {} -Seg (DoubleX1,DoubleX2,DoubleYintd): X1 (x1), X2 (x2), Y (y), d (d) {} - BOOL operator< (ConstSeg &a)Const{ + returny<a.y; - } + }s[max]; A at voidUpdatecur (intNintLeftintRight ) { - if(Flag[n]) sum[n] = x[right+1]-x[left];//indicates that the entire segment length of the interval can be used as the bottom - Else if(left = right) sum[n] =0;//the leaf node is 0 (the length of the segment within the interval is 0). - ElseSum[n] = sum[n<<1] + sum[n<<1|1]; - } - in voidUpdateintOintLintRintS,intTintd) { - if(S<=l && r<=t) {//This interval is part of the current scan segment, which updates the bottom edge of the interval and the difference in the top and bottom edges toFlag[o] + = D;//update the number of difference between bottom +Updatecur (O, L, R);//Update Bottom Length - return; the } * intMid = (L + r) >>1, ls = o<<1, rs = o<<1|1; $ if(Mid >=s) update (LS, L, Mid, S, T, D);Panax Notoginseng if(mid+1<= t) update (RS, mid+1, R, S, T, D); - updatecur (O, L, R); the } + A intBin_search (DoubleKey,intN) { the intL =0, R = N1; + while(l<=R) { - intMid = (l+r) >>1; $ if(X[mid] = = key)returnmid; $ if(X[mid] > key) r=mid-1; - ElseL=mid+1; - } the return-1; - }Wuyi the intMain () - { Wu //freopen ("a.in", "R", stdin); - intn,cas=0; About DoubleX1,x2,y1,y2; $ while(SCANF ("%d", &N), N) { - intk=0; - for(intI=0; i<n;++i) { -Cin>>x1>>y1>>x2>>Y2; Ax[k]=X1; +S[k++]=seg (X1,x2,y1,1); thex[k]=x2; -S[k++]=seg (x1,x2,y2,-1); $ } theSort (x,x+k); theSort (s,s+k); the intt = unique (x, x+k)-x; the Doubleans=0; - in for(intI=0; i<k;++i) {//Scan Segments the intPOS1 =Bin_search (s[i].x1, t); the /* About This is because it is understood to be a legal line segment, so the right-most point is the first I-dot, corresponding to the first i-1 edge the Similarly, in Updatecur, the calculation of the effective segment length requires x[r+1] the */ the intPos2 = Bin_search (s[i].x2, T)-1; +Update1,0, T1, POS1, Pos2, S[I].D);//update the bottom and bottom edge lengths when scanning segments -Ans + = sum[1]* (s[i+1].Y-S[I].Y);//newly added area the }Bayiprintf"Test case #%d\ntotal explored area:%.2lf\n\n",++Cas,ans); the } the return 0; -}
HDU 1542 Atlantics segment tree + discrete scan