描述
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.
样例输入
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
题意
给出一个矩阵,多次查询某子矩阵的最大值,并判断其是否在子矩阵顶点上;
题解
这道题,显然是一个RMQ;
把RMQ改成二维的就可以了;
代码题。。反正我是RE++;
多组数据
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define N 303
using namespace std;
int rmq[N][N][9][9];
int main()
{
int n,m,q,i,j,k,l,x1,y1,x2,y2;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(rmq,0,sizeof(rmq));
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&rmq[i][j][0][0]);
}
}
for(l=1;l<=8;l++)
for(i=1;i<=m;i++)
for(j=1;j+(1<<l-1)<=n;j++)
rmq[i][j][0][l]=max(rmq[i][j][0][l-1],rmq[i][j+(1<<(l-1))][0][l-1]);
for(k=1;k<=8;k++)
for(i=1;i+(1<<k-1)<=m;i++)
for(j=1;j<=n;j++)
rmq[i][j][k][0]=max(rmq[i][j][k-1][0],rmq[i+(1<<(k-1))][j][k-1][0]);
for(k=1;k<=8;k++)
{
for(l=1;l<=8;l++)
{
for(i=1;i+(1<<k-1)<=m;i++)
{
for(j=1;j+(1<<l-1)<=n;j++)
{
rmq[i][j][k][l]=max(rmq[i][j][k-1][l-1],max(rmq[i+(1<<(k-1))][j][k-1][l-1],max(rmq[i][j+(1<<(l-1))][k-1][l-1],rmq[i+(1<<(k-1))][j+(1<<(l-1))][k-1][l-1])));
}
}
}
}
scanf("%d",&q);
for(i=1;i<=q;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
j=log(x2-x1+1)/log(2);
k=log(y2-y1+1)/log(2);
l=max(rmq[x1][y1][j][k],max(rmq[x2-(1<<j)+1][y1][j][k],max(rmq[x1][y2-(1<<k)+1][j][k],rmq[x2-(1<<j)+1][y2-(1<<k)+1][j][k])));
printf("%d ",l);
if(rmq[x1][y1][0][0]==l||rmq[x1][y2][0][0]==l||rmq[x2][y1][0][0]==l||rmq[x2][y2][0][0]==l)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
}
}