Main topic
The graph of the overlapping of n rectangles is given, which requires the contour line length after overlapping.
Exercises
Referring to the 1999 national training Team Chen Hong's paper "Data structure selection and algorithm efficiency-from the IOI98 question picture".
It's very clear, it's not detailed here.
is to abstract the picture into a model of the number of statistical intervals. Simply and rudely using line-segment tree + discretization.
Code
/*task:picturelang:c++*/#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespacestd;Const intMAXN =5005;Const intRANGE =10001;structmatrix{intx[2], y[2];} MATRIX[MAXN];structline{inth, L, R, Delx; BOOL operator< (ConstLine &b)Const { returnH <b.h; }}LINE[MAXN<<1];intN, ans, NL, xcnt, ycnt;intLeftv[range <<5], Rightv[range <<5], Lazytag[range <<5], Minv[range <<5], Maxv[range <<5];voidMaintain (into) { intLC = O <<1, rc = LC +1; Leftv[o]=LEFTV[LC]; Rightv[o]=RIGHTV[RC]; Minv[o]=min (MINV[LC], MINV[RC]); Maxv[o]=Max (MAXV[LC], MAXV[RC]);}voidPushdown (into) { intLC = O <<1, rc = LC +1; if(Lazytag[o]) {MINV[LC]+=Lazytag[o]; MAXV[LC]+=Lazytag[o]; MINV[RC]+=Lazytag[o]; MAXV[RC]+=Lazytag[o]; LEFTV[LC]+=Lazytag[o]; LEFTV[RC]+=Lazytag[o]; RIGHTV[LC]+=Lazytag[o]; RIGHTV[RC]+=Lazytag[o]; LAZYTAG[LC]+=Lazytag[o]; LAZYTAG[RC]+=Lazytag[o]; Lazytag[o]=0; }}voidUpdateintOintLintRintY1,intY2,intDX) { if(Y1 <= L && R <=y2) {Leftv[o]+=DX; Rightv[o]+=DX; Minv[o]+=DX; Maxv[o]+=DX; Lazytag[o]+=DX; return; } pushdown (o); intMid = (L + R) >>1; intLC = O <<1, rc = LC +1; if(Mid >=y1) Update (LC, L, Mid, y1, y2, dx); if(Mid +1<= y2) update (RC, Mid +1, R, y1, y2, dx); Maintain (o);}intQueryintOintLintR) { if(Minv[o] >0)return 1; if(Maxv[o] = =0)return 0; Pushdown (o); intMid = (L + R) >>1; intLC = O <<1, rc = LC +1; intA = query (LC, L, mid), B = Query (RC, Mid +1, R); if(RIGHTV[LC] && LEFTV[RC])returnA + B-1; Else returnA +b;}voidWorkintcur) {NL=0; for(inti =0; I < n; ++i)if(cur = =0) {line[nl].h= matrix[i].x[0]; LINE[NL].L= matrix[i].y[0]; LINE[NL].R= matrix[i].y[1]; LINE[NL++].delx =1; Line[nl].h= matrix[i].x[1]; LINE[NL].L= matrix[i].y[0]; LINE[NL].R= matrix[i].y[1]; LINE[NL++].delx =-1; } Else{line[nl].h= matrix[i].y[0]; LINE[NL].L= matrix[i].x[0]; LINE[NL].R= matrix[i].x[1]; LINE[NL++].delx =1; Line[nl].h= matrix[i].y[1]; LINE[NL].L= matrix[i].x[0]; LINE[NL].R= matrix[i].x[1]; LINE[NL++].delx =-1; } sort (line, line+NL); memset (LEFTV,0,sizeof(LEFTV)); memset (Rightv,0,sizeof(RIGHTV)); memset (MAXV,0,sizeof(MAXV)); memset (MINV,0,sizeof(MINV)); memset (Lazytag,0,sizeof(Lazytag)); memset (MINV,0,sizeof(MINV)); for(inti =0; I < NL-1; ++i) {Update (1,1,4* RANGE,2* (LINE[I].L + RANGE-1),2* (LINE[I].R + RANGE-1), LINE[I].DELX); Ans+ = Query (1,1,4* RANGE) *2* (Line[i +1].h-line[i].h); }}intMain () {Freopen ("picture.in","R", stdin); Freopen ("Picture.out","W", stdout); scanf ("%d", &N); Xcnt= YCNT =0; for(inti =0; I < n; ++i) scanf ("%d%d%d%d", &matrix[i].x[0], &matrix[i].y[0], &matrix[i].x[1], &matrix[i].y[1]); Ans=0; Work (0); Work (1); printf ("%d\n", ans); return 0;}
"Usaco 5.5.1" picture