题干:
FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find.
FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.
FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.
Input
* Line 1: Three space-separated integers: N, B, and K.
* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.
* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.
Output
* Lines 1..K: A single integer per line representing the difference between the max and the min in each query.
Sample Input
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
Sample Output
5
题目大意:
给出一个N*N (N<=250)的方阵,以及K(<=100000)个询问。每次询问如下:以(Xi,Yi)为左上角,边长为B的子方阵中,最大值和最小值的差是多少?注意对于所有的询问,B都是一个定值。
Input
第一行N,B(<=N),K。含义如上。
接下来N行N列的一个矩阵,每个数<=250。
接下来K行表示询问,每行两个数Xi, Yi 表示询问的方阵的左上角。
解题报告:
首先这题做法贼鸡儿多,因为是练模板嘛所以我就选了不是最快的方式。最快的方式好像是单调队列,也能做到回答O1查询。
注意不能先枚举x再枚举j、、我真是个麻瓜、、倍增这里的问题犯过一次了。(我记得是LCA的时候应该先dfs进去然后再处理而我是先处理了然后再dfs子树的)
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 300 + 5;
int Log[MAX];
int F[MAX][MAX][22],f[MAX][MAX][22],a[MAX][MAX];
int n,B,K;
PII Min_Max(int x,int y,int z,int q) {int b[4] = {x,y,z,q};sort(b,b+4);return pm(b[0],b[3]);
}
void ST() {for(int j = 1; (1<<j) <=n; j++) {for(int x = 1; x<=n; x++) {int t = 1<<j-1;for(int y = 1; y+(1<<j) - 1 <= n; y++) {F[x][y][j] = Min_Max(F[x][y][j-1],F[x][y+t][j-1],F[x+t][y][j-1],F[x+t][y+t][j-1]).SS;f[x][y][j] = Min_Max(f[x][y][j-1],f[x][y+t][j-1],f[x+t][y][j-1],f[x+t][y+t][j-1]).FF;}}}
}
int solve(int x,int y) {int k = (int)(log(B)/log(2));//Log[B];int mx = Min_Max(F[x][y][k],F[x+B-(1<<k)][y][k],F[x][y+B-(1<<k)][k],F[x+B-(1<<k)][y+B-(1<<k)][k]).SS;int mn = Min_Max(f[x][y][k],f[x+B-(1<<k)][y][k],f[x][y+B-(1<<k)][k],f[x+B-(1<<k)][y+B-(1<<k)][k]).FF;return mx - mn;
}
int main()
{cin>>n>>B>>K;for(int i = 2; i<=n; i++) Log[i] = Log[i>>1] + 1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) scanf("%d",&a[i][j]),F[i][j][0]=f[i][j][0]=a[i][j]; } ST();int x,y;while(K--) {scanf("%d%d",&x,&y);printf("%d\n",solve(x,y));}return 0 ;
}
/*
3 2 10
1 2 3
4 5 5
7 5 5
1 1
1 2
2 1
2 2*/