/* 就是多了个比较的操作
* File: main.cpp
* Author: lenovo
*
* Created on 2011年10月13日, 下午10:26
*/
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
/*这个题目是个二维rmq的模板题
*和一维rmq的思路一样
*用dp[row][col][i][j]表示(row,col)到(row+2^i,col+2^j)矩形内的最小值
*查询的时候
* int kx = log(double(x2 - x1 +1)) / log(2.0);
int ky = log(double(y2 - y1 +1)) / log(2.0);
int m1 = dp[x1][y1][kx][ky];
int m2 = dp[x2-(1<<kx)+1][y1][kx][ky];
int m3 = dp[x1][y2-(1<<ky)+1][kx][ky];
int m4 = dp[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];
*取4个值里面的最小值(有种二分的思想)
*/
const int maxn = 301;
int mat[maxn][maxn];
int dp[maxn][maxn][9][9];
int n , m , q , flag;
void rmq_2d(void)
{
for(int row = 1 ; row <= m ; row++)
for(int col = 1 ; col <= n ; col++)
dp[row][col][0][0] = mat[row][col];
int t = log((double)n) / log(2.0);
for(int i = 0 ; i <= t ; i++)
{
for(int j = 0 ; j <= t ; j++)
{
if(i == 0 && j == 0)
continue;
for(int row = 1 ; row+(1<<i)-1 <= m ; row++)
{
for(int col = 1 ; col+(1<<j)-1 <= n ; col++)
{
if(i == 0)
{
dp[row][col][i][j] = max(dp[row][col][i][j-1] , dp[row][col+(1<<(j-1))][i][j-1]);
}
else
{
dp[row][col][i][j] = max(dp[row][col][i-1][j] , dp[row+(1<<(i-1))][col][i-1][j]);
}
}
}
}
}
}
int query_2d(int x1,int x2,int y1,int y2)
{
int kx = log(double(x2 - x1 +1)) / log(2.0);
int ky = log(double(y2 - y1 +1)) / log(2.0);
int m1 = dp[x1][y1][kx][ky];
int m2 = dp[x2-(1<<kx)+1][y1][kx][ky];
int m3 = dp[x1][y2-(1<<ky)+1][kx][ky];
int m4 = dp[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];
int ans = max( max(m1,m2), max(m3,m4) );
if(ans == mat[x1][y1] || ans == mat[x1][y2] || ans == mat[x2][y1] || ans == mat[x2][y2])
flag = 1;
return ans;
}
int main(int argc, char** argv) {
while(scanf("%d%d",&m,&n) != EOF){
for(int i = 1 ; i <= m ; i++)
for(int j = 1 ; j <= n ; j++)
scanf("%d",&mat[i][j]);
rmq_2d();
scanf("%d",&q);
while(q--){
int x1 , x2 , y1 , y2 ;
flag = 0;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%d ",query_2d(x1,x2,y1,y2));
if(flag)
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}
转载于:https://www.cnblogs.com/zuckerTT/archive/2011/10/13/2210982.html