1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define INIT(a,b) memset(a,b,sizeof(a)) #define LL long long int using namespace std; const int MAX=0x7fffffff; const int MIN=-0x7fffffff; const int INF=0x3f3f3f3f; const int Mod=1e9+7; const int maxn=300; int n,b,k; int a[maxn][maxn],st1[maxn][maxn][30],st2[maxn][maxn][30];; void Init(){ for(int i=1;i<=n;i++) for(int k=1;(1<<k)<=n;k++) for(int j=1;j+(1<<k)-1<=n;j++){ st1[i][j][k]=max(st1[i][j][k-1],st1[i][j+(1<<k)-1][k-1]); st2[i][j][k]=min(st2[i][j][k-1],st2[i][j+(1<<k)-1][k-1]); } } int main(){ while(~scanf("%d %d %d",&n,&b,&k)){ INIT(st1,128);INIT(st2,INF); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); st1[i][j][0]=a[i][j]; st2[i][j][0]=a[i][j]; } Init(); int x,y; while(k--){ scanf("%d%d",&x,&y); int ans1=MIN,ans2=MAX; int k=log(double(b))/log(2.0); int l=y,r=y+b-1; for(int i=x;i<x+b;i++){ ans1=max(ans1,max(st1[i][l][k],st1[i][r-(1<<k)+1][k])); ans2=min(ans2,min(st2[i][l][k],st2[i][r-(1<<k)+1][k])); } printf("%d\n",ans1-ans2); } }
return 0; }
|