ACM-ICPC Asia (Nanning Division) online tournament overlapping rectangles rectangular area and

Source: Internet
Author: User

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, &AMP;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

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.