/*Rokua U4792 acheing Two-dimensional line segment tree N*n*logn*logn t into a dumb force 2333*/#include<iostream>#include<cstdio>#include<cstring>#defineMAXN 1010#defineLC K*2#defineRC K*2+1#defineMid (L+r)/2using namespacestd;intn,m,k,g[maxn][maxn],x,y,z;intx1,x2,y1,y2,ans=0x7fffffff;intmx[maxn*4][maxn*4],mxx[maxn*4][maxn*4];voidInsert_y (intKxintKintLintR) {Mx[kx][k]=min (mx[kx][k],z); MXX[KX][K]=Max (mxx[kx][k],z); if(L==R)return; if(y<=mid) Insert_y (Kx,lc,l,mid); ElseInsert_y (kx,rc,mid+1, R); MX[KX][K]=min (MX[KX][LC],MX[KX][RC]); MXX[KX][K]=Max (MXX[KX][LC],MXX[KX][RC]);}voidInsert_x (intKintLintR) {insert_y (k,1,1, M); if(L==R)return; if(x<=mid) Insert_x (Lc,l,mid); ElseInsert_x (rc,mid+1, R);}intQuery_miny (intKxintKintLintR) { if(Y1<=L&&Y2>=R)returnMx[kx][k]; if(Y2<=mid)returnQuery_miny (Kx,lc,l,mid); Else if(Y1>mid)returnQuery_miny (kx,rc,mid+1, R); Else returnMin (Query_miny (kx,lc,l,mid), Query_miny (kx,rc,mid+1, R));}intQuery_maxy (intKxintKintLintR) { if(Y1<=L&&Y2>=R)returnMxx[kx][k]; if(Y2<=mid)returnQuery_maxy (Kx,lc,l,mid); Else if(Y1>mid)returnQuery_maxy (kx,rc,mid+1, R); Else returnMax (Query_maxy (Kx,lc,l,mid), Query_maxy (kx,rc,mid+1, R));}intQuery_minx (intKintLintR) { if(X1<=L&&X2>=R)returnQuery_miny (k,1,1, M); if(X2<=mid)returnQuery_minx (Lc,l,mid); Else if(X1>mid)returnQuery_minx (rc,mid+1, R); Else returnMin (Query_minx (lc,l,mid), Query_minx (rc,mid+1, R));}intQuery_maxx (intKintLintR) { if(X1<=L&&X2>=R)returnQuery_maxy (k,1,1, M); if(X2<=mid)returnQuery_maxx (Lc,l,mid); Else if(X1>mid)returnQuery_maxx (rc,mid+1, R); Else returnMax (Query_maxx (Lc,l,mid), Query_maxx (rc,mid+1, R));}intMain () {scanf ("%d%d%d",&n,&m,&k); memset (Mxx,-127/3,sizeof(MXX)); memset (MX,127/3,sizeof(MX)); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {scanf ("%d",&G[i][j]); X=i;y=j;z=G[i][j]; Insert_x (1,1, N); } for(inti=k;i<=n;i++) for(intj=k;j<=m;j++) {x1=i-k+1; x2=i;y1=j-k+1; y2=J; intMinner=query_minx (1,1, N); intMaxxer=query_maxx (1,1, N); Ans=min (ans,maxxer-Minner); } printf ("%d\n", ans); return 0;}
/*Rokua U4792 acheing monotone queue using to solve the problem of two-dimensional segment tree Timeout*/#include<cstdio>#include<cstring>#defineMAXN 1010using namespacestd;intN,M,K,G[MAXN][MAXN],MX[MAXN][MAXN],MI[MAXN][MAXN],MXX[MAXN][MAXN],MXI[MAXN][MAXN];intq[maxn],head,tail,ans=0x7fffffff;intinit () {intx=0, f=1;Chars=GetChar (); while(s<'0'|| S>'9'){if(s=='-') f=-1; s=GetChar ();} while(s>='0'&&s<='9') {x=x*Ten+s-'0'; s=GetChar ();} returnx*F;}intMinintXinty) { returnX<y?x:y;}voidReady () { for(intj=1; j<=m;j++) {Head=1; tail=0; for(intI=1; i<=n;i++){ intx=G[i][j]; while(Head<=tail&&x>g[q[tail]][j]) tail--; q[++tail]=i; if(i-q[head]>=k) head++; MX[I][J]=G[q[head]][j]; } } for(intj=1; j<=m;j++) {Head=1; tail=0; for(intI=1; i<=n;i++){ intx=G[i][j]; while(Head<=tail&&x<g[q[tail]][j]) tail--; q[++tail]=i; if(i-q[head]>=k) head++; MI[I][J]=G[q[head]][j]; } }}voidSolve () { for(inti=k;i<=n;i++) {Head=1; tail=0; for(intj=1; j<=m;j++){ intx=Mx[i][j]; while(Head<=tail&&x>mx[i][q[tail]]) tail--; q[++tail]=J; if(j-q[head]>=k) head++; MXX[I][J]=Mx[i][q[head]]; } } for(inti=k;i<=n;i++) {Head=1; tail=0; for(intj=1; j<=m;j++){ intx=Mi[i][j]; while(Head<=tail&&x<mi[i][q[tail]]) tail--; q[++tail]=J; if(j-q[head]>=k) head++; MXI[I][J]=Mi[i][q[head]]; } }}intMain () {n=init (); M=init (); k=init (); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) G[i][j]=init (); Ready (); Solve (); for(inti=k;i<=n;i++) for(intj=k;j<=m;j++) ans=min (ans,mxx[i][j]-Mxi[i][j]); printf ("%d\n", ans); return 0;}
Rokua U4792 acheing Monotone queue