题目大意:给你一个n*n的矩阵,每次给你一个点(x,y),以其为左上角,宽度为b的矩阵中最小的数值和最大数值的差是多少? 一共k个询问。
思路:简单的二维st。
定义dp(i,j,k,L)表示以(i,j)为左上角,宽度为(2^k, 2^L)的区间内的最大(小)值。
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<iostream>
#include<utility>
#include<stdlib.h>
#include<time.h>
#include<cmath>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int dp[maxn][maxn][][], dp2[maxn][maxn][][];
int n, b, k; void init_st(){
for (int i = ; ( << i) <= n; i++){
for (int j = ; ( << j) <= n; j++){
if (i == && j == ) continue;
for (int x = ; x + ( << i) - <= n; x++){
for (int y = ; y + ( << j) - <= n; y++){
if (i == ) {
dp[x][y][i][j] = min(dp[x][y][i][j - ], dp[x][y + ( << (j - ))][i][j - ]);
dp2[x][y][i][j] = max(dp2[x][y][i][j - ], dp2[x][y + ( << (j - ))][i][j - ]);
}
else {
dp[x][y][i][j] = min(dp[x][y][i - ][j], dp[x + ( << (i - ))][y][i - ][j]);
dp2[x][y][i][j] = max(dp2[x][y][i - ][j], dp2[x + ( << (i - ))][y][i - ][j]);
}
}
}
}
}
} int query(int x, int y, int X, int Y){
int maxi = , mini = 1e8;
int k1 = , k2 = ;
while ( << ( + k1) <= X - x) k1++;
while ( << ( + k2) <= Y - y) k2++;
maxi = max(maxi, max(dp2[x][y][k1][k2], dp2[X - ( << k1) + ][y][k1][k2]));
maxi = max(maxi, max(dp2[x][Y - ( << k2) + ][k1][k2], dp2[X - ( << k1) + ][Y - ( << k2) + ][k1][k2])); mini = min(mini, min(dp[x][y][k1][k2], dp[X - ( << k1) + ][y][k1][k2]));
mini = min(mini, min(dp[x][Y - ( << k2) + ][k1][k2], dp[X - ( << k1) + ][Y - ( << k2) + ][k1][k2]));
//printf("maxi = %d mini = %d\n", maxi, mini);
return maxi - mini;
} int main(){
//while (true){
cin >> n >> b >> k;
for (int i = ; i <= n; i++){
for (int j = ; j <= n; j++){
scanf("%d", &dp[i][j][][]);
dp2[i][j][][] = dp[i][j][][];
}
}
init_st();
for (int i = ; i <= k; i++){
int x, y; scanf("%d%d", &x, &y);
int ans = query(x, y, x + b - , y + b - );
printf("%d\n", ans);
}
//}
return ;
}