HDU2888 Check Corners(二维RMQ)

有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上

裸的二维RMQ

 #pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1, ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --) template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = + ;
const int MAXM = ;
const double eps = 1e-;
LL MOD = ; int m, n;
int mx[][][][];
int idx[], q, lx, ly, rx, ry; void rmq_init(int m, int n) {
for(int i = ; (<<i) <= m; i ++) {
for(int j = ; (<<j) <= n; j ++) {
if(i == && j == ) continue;
int len2 = ( << j), len1 = ( << i);
for(int x = ; x + len1 - <= m; x ++) {
for(int y = ; y + len2 - <= n; y ++) {
if(i == ) mx[x][i][y][j] = max(mx[x][i][y][j - ], mx[x][i][y + (len2 >> )][j - ]);
else mx[x][i][y][j] = max(mx[x][i - ][y][j], mx[x + (len1 >> )][i - ][y][j]);
}
}
}
}
for(int i = ; i <= m || i <= n; i ++) {
idx[i] = ;
while(( << (idx[i] + )) <= i) idx[i] ++;
}
} int rmq(int lx, int rx, int ly, int ry) {
int a = idx[rx - lx + ], la = ( << a);
int b = idx[ry - ly + ], lb = ( << b);
return max(max(max(mx[lx][a][ly][b],
mx[rx - la + ][a][ly][b]),
mx[lx][a][ry - lb + ][b]),
mx[rx - la + ][a][ry - lb + ][b]);
} int main()
{
//FIN;
while(~scanf("%d %d", &m, &n)) {
mem0(mx);
rep (i, , m) rep (j, , n)
scanf("%d", &mx[i][][j][]);
rmq_init(m, n);
scanf("%d", &q);
while(q--) {
scanf("%d %d %d %d", &lx, &ly, &rx, &ry);
int ma = rmq(lx, rx, ly, ry);
printf("%d %s\n", ma, ma == mx[lx][][ly][] || ma == mx[lx][][ry][]
|| ma == mx[rx][][ly][] || ma == mx[rx][][ry][]
? "yes" : "no");
}
}
return ;
}
上一篇:hdu2888 二维RMQ


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