5 3 1 5 1 2 6 3 1 3 5 2 7 7 2 4 6 1 9 9 8 6 5 0 6 9 3 9 1 2
5
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 252; 18 int maxv[maxn][maxn][11],minv[maxn][maxn][11]; 19 int n,d,q,s,t; 20 int main() { 21 while(~scanf("%d %d %d",&n,&d,&q)) { 22 for(int i = 0; i < n; i++) 23 for(int j = 0; j < n; j++) { 24 scanf("%d",&maxv[i][j][0]); 25 minv[i][j][0] = maxv[i][j][0]; 26 } 27 for(int i = 0; i < n; i++){ 28 for(int k = 1; (1<<k) < n; k++){ 29 for(int j = 0; j + (1<<k) <= n; j++){ 30 maxv[i][j][k] = max(maxv[i][j][k-1],maxv[i][j+(1<<(k-1))][k-1]); 31 minv[i][j][k] = min(minv[i][j][k-1],minv[i][j+(1<<(k-1))][k-1]); 32 } 33 } 34 } 35 int z = log2(d),theMin,theMax; 36 while(q--){ 37 scanf("%d %d",&s,&t); 38 --s; 39 --t; 40 theMin = INF; 41 theMax = 0; 42 for(int i = s; i < s + d; i++){ 43 theMin = min(theMin,min(minv[i][t][z],minv[i][t + d - (1<<z)][z])); 44 theMax = max(theMax,max(maxv[i][t][z],maxv[i][t + d - (1<<z)][z])); 45 } 46 printf("%d\n",theMax - theMin); 47 } 48 } 49 return 0; 50 }
原文:http://www.cnblogs.com/crackpotisback/p/4004508.html