Check Corners
Problem Description
Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numbers ( 1 <= i <= m, 1 <= j <= n ). Now he selects some sub-matrices, hoping to find the maximum number. Then he finds that there may be more than one maximum number, he also wants to know the number of them. But soon he find that it is too complex, so he changes his mind, he just want to know whether there is a maximum at the four corners of the sub-matrix, he calls this “Check corners”. It’s a boring job when selecting too many sub-matrices, so he asks you for help. (For the “Check corners” part: If the sub-matrix has only one row or column just check the two endpoints. If the sub-matrix has only one entry just output “yes”.)
Input
There are multiple test cases.
For each test case, the first line contains two integers m, n (1 <= m, n <= 300), which is the size of the row and column of the matrix, respectively. The next m lines with n integers each gives the elements of the matrix which fit in non-negative 32-bit integer.
The next line contains a single integer Q (1 <= Q <= 1,000,000), the number of queries. The next Q lines give one query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <= r2 <= m, 1 <= c1 <= c2 <= n), which are the indices of the upper-left corner and lower-right corner of the sub-matrix in question.
Output
For each test case, print Q lines with two numbers on each line, the required maximum integer and the result of the “Check corners” using “yes” or “no”. Separate the two parts with a single space.
Sample Input
4 4
4 4 10 7
2 13 9 11
5 7 8 20
13 20 8 2
4
1 1 4 4
1 1 3 3
1 3 3 4
1 1 1 1
Sample Output
20 no
13 no
20 yes
4 yes
Source
2009 Multi-University Training Contest 9 - Host by HIT
题目类型:二维RMQ
算法分析:直接使用二维RMQ计算即可
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 |
#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 = 366 + 6; int ans[maxn][maxn]; int dpmax[maxn][maxn][9][9];//最大值 void RMQ_ST (int n, int m) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) dpmax[i][j][0][0] = ans[i][j]; int mx = log(double(n)) / log(2.0); int my = log(double(m)) / log(2.0); for (int i = 0; i <= mx; i++) { for (int j = 0; j <= my; j++) { if (i == 0 && j ==0) continue; for (int p = 1; p + (1 << i) - 1 <= n; p++) { for(int q = 1; q + (1 << j) - 1 <= m; q++) { if (i == 0)//y轴二分 dpmax[p][q][i][j] = max (dpmax[p][q][i][j-1], dpmax[p][q+(1<<(j-1))][i][j-1]); else//x轴二分 dpmax[p][q][i][j] = max (dpmax[p][q][i-1][j], dpmax[p+(1<<(i-1))][q][i-1][j]); } } } } } int RMQ_Findmax (int x1, int y1, int x2, int y2) { int kx = log(double(x2-x1+1)) / log(2.0); int ky = log(double(y2-y1+1)) / log(2.0); int m1 = dpmax[x1][y1][kx][ky]; int m2 = dpmax[x2-(1<<kx)+1][y1][kx][ky]; int m3 = dpmax[x1][y2-(1<<ky)+1][kx][ky]; int m4 = dpmax[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky]; return max (max(m1, m2), max (m3, m4)); } int main() { // freopen ("aaa.txt", "r", stdin); int x1, x2, y1, y2, m, n, q; while (scanf ("%d%d", &n, &m) != EOF) { for (int i = 1; i<= n; i++) for (int j = 1; j <= m; j++) scanf ("%d", &ans[i][j]); RMQ_ST (n, m); scanf ("%d", &q); for (int i = 1; i <= q; i++) { scanf ("%d%d%d%d", &x1, &y1, &x2, &y2); int temp = RMQ_Findmax (x1, y1, x2, y2); printf ("%d ", temp); if (ans[x1][y1] == temp || ans[x2][y1] == temp || ans[x1][y2] == temp || ans[x2][y2] == temp) puts ("yes"); else puts ("no"); } } return 0; } |
- « 上一篇:hdu2815
- hdu2896:下一篇 »