Overlapping rectangles
Test instructions: Find n rectangles and area and
Tags: Scan line + line tree, template title
Reference Blog
#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineRep (i,a,b) for (int i=a; i<=b; ++i)#definePer (i,b,a) for (int i=b; i>=a;-I.)#defineMes (A, b) memset (A,b,sizeof (a))#defineINF 0x3f3f3f3f#defineMP Make_pair#definePB push_back#defineFi first#defineSe SecondtypedefLong Longll;Const intn= ++5;intcol[n<<3], CNT, res;Doublex[n<<3], sum[n<<3], sum2[n<<3];//open one more times, because there are two x coordinatesstructSeg {DoubleL, R, H; intFlag; SEG () {} seg (DoubleLDoubleRDoubleHintflag): L (L), R (R), H (H), flag (flag) {}BOOL operator< (ConstSeg &Object)Const{ returnH <Object. h; }}s[n<<2];voidPushup (intSomintLintR) { if(Col[ro])//Overwrite one timesum[ro]=x[r+1]-X[l]; Else if(L==R) sum[ro]=0; Elsesum[ro]=sum[ro<<1]+sum[ro<<1|1]; if(col[ro]>=2)//cover more than two timessum2[ro]=x[r+1]-X[l]; Else if(L==R) sum2[ro]=0; Else if(col[ro]==1) sum2[ro]=sum[ro<<1] +sum[ro<<1|1]; Else if(col[ro]==0) sum2[ro]=sum2[ro<<1] +sum2[ro<<1|1];}voidUpdateintLintRintCintRointLintR//[L,r] current interval, [L,r] target range{ if(L<=l && r<=R) {Col[ro]+=C; Pushup (RO, L, R); return ; } intMid = (l+r) >>1; if(l<=mid) Update (L, R, C, ro<<1, L, mid); if(r>mid) Update (L, R, C, ro<<1|1, mid+1, R); Pushup (RO, L, r);}intBinary_find (Doublex) { intL=1, r=res, ans, mid; while(l<=r) {Mid= (l+r) >>1; if(X[mid] >= X) Ans=mid, r=mid-1; ElseL=mid+1; } returnans;}DoubleSolveintN) {CNT= Res =0; for(intI=1; i<=n; ++i) {Doublex1, y1, x2, y2; scanf ("%lf%lf%lf%lf", &x1, &y1, &X2, &y2); s[++cnt]=seg (X1,x2,y1,1); X[CNT]=X1; s[++cnt]=seg (x1,x2,y2,-1); X[CNT]=x2; } sort (X+1, x+1+CNT); Sort (S+1, s+1+CNT); ++Res; for(intI=2; i<=cnt; ++i) {//if the x-coordinate is too large, it will be discretized . if(x[i]!=x[i-1]) x[++res]=X[i]; } memset (Sum,0,sizeof(Sum)); memset (col,0,sizeof(col)); memset (Sum2,0,sizeof(Sum2)); Doubleans=0; for(intI=1; i<cnt; ++i) {intL = Binary_find (S[I].L);//Two-point left end intR = Binary_find (S[I].R)-1;//left closed right open, two points right endUpdate (L, R, S[i].flag,1,1, RES); Ans+ = sum[1]* (s[i+1].H-S[I].H);//Matrix and//ans + = sum2[1]* (s[i+1].h-s[i].h);//Matrix intersection } returnans;}intMain () {intN; while(SCANF ("%d", &N) {printf ("%.0f\n", Solve (n)); } puts ("*"); return 0;}
ACM-ICPC Asia (Nanning Division) online tournament overlapping rectangles rectangular area and