题意: 求最大子矩形 满足 子矩形的最大值-最小值 小于m
用单调队列维护 stl 的双端队列会超时
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define ll long long #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) #define pb push_back #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) typedef pair<int,int>pii; ////////////////////////////////// const int N=500+10; int qmax[N],qmin[N]; int mx[N],mi[N],ans,mp[N][N],n,m; int main() { int cas;cin>>cas; while(cas--) { scanf("%d%d",&n,&m); rep(i,1,n)rep(j,1,n)scanf("%d",&mp[i][j]); int ans=0; for(int u=1;u<=n;u++){ for(int i=1;i<=n;++i)mx[i]=0,mi[i]=inf; for(int d=u;d<=n;++d){ for(int k=1;k<=n;k++)mi[k]=min(mi[k],mp[d][k]),mx[k]=max(mx[k],mp[d][k]); int l1=1,l2=1,r1=0,r2=0; for(int r=1,l=1;r<=n;r++) { while(l1<=r1&&mx[qmax[r1]]<=mx[r])r1--; while(l2<=r2&&mi[qmin[r2]]>=mi[r])r2--; qmax[++r1]=r;qmin[++r2]=r; while(l<=r&&mx[qmax[l1]]-mi[qmin[l2]]>m) { while(l1<=r1&&qmax[l1]<=l)l1++; while(l2<=r2&&qmin[l2]<=l)l2++; l++; } ans=max(ans,(r-l+1)*(d-u+1)); } } } printf("%d ",ans); } return 0; }