#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <set> #include <vector> #include <map> #include <queue> #include <set> #include <algorithm> using namespace std; #define MAXN 310 int rec[MAXN][MAXN]; int dp[MAXN][MAXN][10][10]; int n,m; int summ[MAXN][MAXN]; int sumn[MAXN][MAXN]; int sum1[MAXN][MAXN]; int sum2[MAXN][MAXN]; inline int maxm(int a,int b,int c,int d){ if(a<b)a=b; if(a<c)a=c; if(a<d)a=d; return a; } void st() { for(int k=0;(1<<k)<=n;k++) for(int l=0;(1<<l)<=m;l++) for(int i=1;i+(1<<k)-1<=n;i++) for(int j=1;j+(1<<l)-1<=m;j++) { if(!k&&!l){ dp[i][j][k][l]=rec[i][j]; } else if(k==0){ dp[i][j][k][l]=max(dp[i][j][k][l-1],dp[i][j+(1<<(l-1))][k][l-1]); } else if(l==0){ dp[i][j][k][l]=max(dp[i][j][k-1][l],dp[i+(1<<(k-1))][j][k-1][l]); } else { dp[i][j][k][l]=maxm(dp[i][j][k-1][l-1],dp[i+(1<<(k-1))][j][k-1][l-1], dp[i][j+(1<<(l-1))][k-1][l-1],dp[i+(1<<(k-1))][j+(1<<(l-1))][k-1][l-1]); } } } int rmq2dmax(int x,int y,int x1,int y1){ int k=log(x1-x+1)/log(2); int l=log(y1-y+1)/log(2); return maxm(dp[x][y][k][l],dp[x1-(1<<k)+1][y][k][l], dp[x][y1-(1<<l)+1][k][l],dp[x1-(1<<k)+1][y1-(1<<l)+1][k][l]); } void pre_do(){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ summ[i][j]=summ[i][j-1]+rec[i][j]; sum1[i][j]=sum1[i-1][j]+summ[i][j]; sumn[i][j]=sumn[i][j-1]+rec[i][j]*rec[i][j]; sum2[i][j]=sum2[i-1][j]+sumn[i][j]; } } int getare1(int x,int y,int x1,int y1){ return sum1[x1][y1]-sum1[x-1][y1]-sum1[x1][y-1]+sum1[x-1][y-1]; } int getare2(int x,int y,int x1,int y1){ return sum2[x1][y1]-sum2[x-1][y1]-sum2[x1][y-1]+sum2[x-1][y-1]; } int main() { memset(summ,0,sizeof(summ)); memset(sum1,0,sizeof(sum1)); int cas=0; while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&rec[i][j]); st(); pre_do(); int q; scanf("%d",&q); printf("Case %d:\n",++cas); int nn,mm; while(q--){ scanf("%d%d",&nn,&mm); int posx=1,posy=1; double fancha=-1.0; for(int x=1;x+nn-1<=n;x++) for(int y=1;y+mm-1<=m;y++){ int ma=rmq2dmax(x,y,x+nn-1,y+mm-1); int a1=getare1(x,y,x+nn-1,y+mm-1)-ma; int a2=getare2(x,y,x+nn-1,y+mm-1)-ma*ma; double _x=(double)a1/(nn*mm-1); double val=1.0*(a2-2*_x*a1+(nn*mm-1)*_x*_x)/(nn*mm-1); if(fancha<0||fancha>val){ fancha=val; posx=x;posy=y; } } if(fancha<0)fancha=0.0; printf("(%d, %d), %.2lf\n",posx,posy,fancha); } } return 0; }
|