Cornfields
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
Source
题目类型:二维RMQ
算法分析:直接使用二维ST来预处理,然后对于每一个查询直接输出结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1000000000 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 266 + 6; int mm[maxn]; int dpmax[maxn][maxn][8][8];//最大值 int dpmin[maxn][maxn][8][8];//最大值 inline void Init () { mm[0] = -1; for (int i = 1; i <= 266; i++) mm[i] = ((i & (i - 1)) == 0) ? mm[i-1] + 1 : mm[i-1]; } void RMQ_ST (int n, int m) { for (int i = 1;i <= n; i++) for (int j = 1; j <= m; j++) { scanf ("%d", &dpmax[i][j][0][0]); dpmin[i][j][0][0] = dpmax[i][j][0][0]; } for (int ii = 0; ii <= mm[n]; ii++) for (int jj = 0; jj <= mm[m]; jj++) if (ii + jj) for (int i = 1; i + (1 << ii) - 1 <= n; i++) for (int j = 1; j + (1 << jj) - 1 <= m; j++) { if (ii) { dpmax[i][j][ii][jj] = max(dpmax[i][j][ii-1][jj],dpmax[i+(1<<(ii-1))][j][ii-1][jj]); dpmin[i][j][ii][jj] = min(dpmin[i][j][ii-1][jj],dpmin[i+(1<<(ii-1))][j][ii-1][jj]); } else { dpmax[i][j][ii][jj] = max(dpmax[i][j][ii][jj-1],dpmax[i][j+(1<<(jj-1))][ii][jj-1]); dpmin[i][j][ii][jj] = min(dpmin[i][j][ii][jj-1],dpmin[i][j+(1<<(jj-1))][ii][jj-1]); } } } //查询矩形的最大值 inline int RMQ_Findmax (int x1, int y1, int x2, int y2) { int k1 = mm[x2-x1+1]; int k2 = mm[y2-y1+1]; x2 = x2 - (1 << k1) + 1; y2 = y2 - (1 << k2) + 1; return max (max (dpmax[x1][y1][k1][k2], dpmax[x1][y2][k1][k2]), max(dpmax[x2][y1][k1][k2], dpmax[x2][y2][k1][k2])); } inline int RMQ_Findmin (int x1, int y1, int x2, int y2) { int k1 = mm[x2-x1+1]; int k2 = mm[y2-y1+1]; x2 = x2 - (1 << k1) + 1; y2 = y2 - (1 << k2) + 1; return min (min (dpmin[x1][y1][k1][k2], dpmin[x1][y2][k1][k2]), min(dpmin[x2][y1][k1][k2], dpmin[x2][y2][k1][k2])); } int main() { // freopen ("aaa.txt", "r", stdin); int x, y, b, n, q; Init (); while (scanf ("%d%d%d", &n, &b, &q) != EOF) { RMQ_ST (n, n); for (int i = 1; i <= q; i++) { scanf ("%d%d", &x, &y); printf ("%d\n", RMQ_Findmax (x, y, x + b - 1, y + b - 1) - RMQ_Findmin (x, y, x + b - 1, y + b - 1)); } } return 0; } |