HDU 2888:Check Corners(二维RMQ)

http://acm.hdu.edu.cn/showproblem.php?pid=2888

题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中的最大值,和判断四个角有没有等于这个最大值的。

思路:二维RMQ模板题。注意内存卡的挺紧的。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 301
int dp[N][N][][], mp[N][N], n, m, x1, x2, y11, y2;
// 内存刚好 30704kB 卡着过了 void Init() {
for(int i = ; i <= n; i++) for(int j = ; j <= m; j++) dp[i][j][][] = mp[i][j];
int A = (int)(log(n) / log()), B = (int)(log(m) / log());
for(int j = ; j <= A; j++) {
for(int y = ; y <= B; y++) {
if(j == && y == ) continue;
for(int i = ; i + ( << j) - <= n; i++)
for(int x = ; x + ( << y) - <= m; x++)
if(j) dp[i][x][j][y] = max(dp[i][x][j-][y], dp[i+(<<(j-))][x][j-][y]);
else dp[i][x][j][y] = max(dp[i][x][j][y-], dp[i][x+(<<(y-))][j][y-]);
}
}
} int Query() {
int A = (int)(log(x2 - x1 + ) / log()), B = (int)(log(y2 - y11 + ) / log());
int a = dp[x1][y11][A][B];
int b = dp[x2-(<<A)+][y11][A][B];
int c = dp[x1][y2-(<<B)+][A][B];
int d = dp[x2-(<<A)+][y2-(<<B)+][A][B];
return max(a, max(b, max(c, d)));
} bool check(int val) {
if(mp[x1][y11] == val || mp[x2][y2] == val || mp[x1][y2] == val || mp[x2][y11] == val) return true;
return false;
} int main() {
while(~scanf("%d%d", &n, &m)) {
for(int i = ; i <= n; i++) for(int j = ; j <= m; j++) scanf("%d", &mp[i][j]);
int q; scanf("%d", &q);
Init();
while(q--) {
scanf("%d%d%d%d", &x1, &y11, &x2, &y2);
int val = Query();
printf("%d ", val);
if(check(val)) puts("yes");
else puts("no");
}
}
return ;
}
上一篇:二维RMQ hdu 2888


下一篇:C语言每日一题之No.7