2014
02-17

# Check Corners

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”.)

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.

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.

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

20 no
13 no
20 yes
4 yes

#include<iostream>
#include<cmath>
using namespace std;
const int maxn = 301;
int val[maxn][maxn];

int M,N;

//RMQ 2D
int dp[maxn][maxn][9][9];
void RMQ_2D_PRE()
{
for(int row = 1; row <= N; row++)
for(int col = 1; col <=M; col++)
dp[row][col][0][0] = val[row][col];
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 row = 1; row+(1<<i)-1 <= N; row++)
{
for(int col = 1; col+(1<<j)-1 <= M; col++)
{
if(i == 0)//y轴二分
dp[row][col][i][j]=max(dp[row][col][i][j-1],dp[row][col+(1<<(j-1))][i][j-1]);
else//x轴二分
dp[row][col][i][j]=max(dp[row][col][i-1][j],dp[row+(1<<(i-1))][col][i-1][j]);
}
}
}
}
}
int RMQ_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];
return max( max(m1,m2) , max(m3,m4));
}
//END

int  main()
{
int Q,x1,y1,x2,y2,i,j;
while(scanf("%d%d",&N,&M)!=EOF)
{
for(i = 1; i <= N; i++)
for(j = 1; j <= M; j++)
scanf("%d",&val[i][j]);
RMQ_2D_PRE();
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = RMQ_2D(x1,x2,y1,y2);
printf("%d",ans);
if(ans == val[x1][y1] || ans == val[x2][y1] || ans == val[x1][y2] || ans == val[x2][y2])
printf(" yes\n");
else printf(" no\n");
}
}
return 0;
}