相比以前的RMQ不同的是,这是一个二维的ST算法
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; #define N 300 int minbest[8][8][N][N]; int maxbest[8][8][N][N]; ///int lg[N]; int getmax(int a,int b,int c,int d) { return max(max(a,b),max(c,d)); } int getmin(int a,int b,int c,int d) { return min(min(a,b),min(c,d)); } int lg(int x) { if(x == 1) return 0; return lg(x>>1) + 1; } int main() { int n,b,k; while(EOF != scanf("%d%d%d",&n,&b,&k)) { //lg[0] = -1; /* for(int i = 1; i <= n; i++) if(i&(i-1) == 0) lg[i] = lg[i-1] + 1; else lg[i] = lg[i-1];*/ for(int i = 1; i <= n; i++) { for(int j = 1; j <= n ; j++) { int num; scanf("%d",&num); minbest[0][0][i][j] = maxbest[0][0][i][j] = num; } } for(int i = 0; (1<<i) <= n ; i++) for(int j = 0; (1<<j) <= n ; j++) { if(i + j) { for(int x = 0; x <= n+1-(1<<i); x++) for(int y = 0; y <= n+1-(1<<j); y++) { if(i) { minbest[i][j][x][y] = min(minbest[i-1][j][x][y],minbest[i-1][j][x+ (1<<i-1)][y]); maxbest[i][j][x][y] = max(maxbest[i-1][j][x][y],maxbest[i-1][j][x+ (1<<i-1)][y]); } if(j) { minbest[i][j][x][y] = min(minbest[i][j-1][x][y],minbest[i][j-1][x][y + (1<<j-1)]); maxbest[i][j][x][y] = max(maxbest[i][j-1][x][y],maxbest[i][j-1][x][y + (1<<j-1)]); } } } } int tmp = lg(b) ; ///cout<<tmp<<endl; while(k--) { int x,y; scanf("%d%d",&x,&y); int mi = getmin(minbest[tmp][tmp][x][y],minbest[tmp][tmp][x+b-(1<<tmp)][y], minbest[tmp][tmp][x][y+b-(1<<tmp)],minbest[tmp][tmp][x+b-(1<<tmp)][y+b-(1<<tmp)] ); int ma = getmax(maxbest[tmp][tmp][x][y],maxbest[tmp][tmp][x+b-(1<<tmp)][y], maxbest[tmp][tmp][x][y+b-(1<<tmp)],maxbest[tmp][tmp][x+b-(1<<tmp)][y+b-(1<<tmp)] ); ///cout<<ma<<" "<<mi<<endl; printf("%d\n",ma - mi); } } return 0; }