Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1646 Accepted Submission(s): 597
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.
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std; const int maxn=;
const int maxm=; int A[maxn][maxn],flag;
int d[maxn][maxn][maxm][maxm]; inline int max(int a,int b){ return a>b?a:b;} void RMQ_init(int n,int m)
{
int i,j,k,l;
for(i=;i<n;i++)
for(j=;j<m;j++) d[i][j][][]=A[i][j];
for(i=;(<<i)<=n;i++)
{
for(j=;(<<j)<=m;j++)
{
if(i== && j==) continue;
for(k=;k+(<<i)-<n;k++)
{
for(l=;l+(<<j)-<m;l++)
{
if(i== && j!=) d[k][l][i][j]=max(d[k][l][i][j-],d[k][l+(<<(j-))][i][j-]);
else if(i!= && j==) d[k][l][i][j]=max(d[k][l][i-][j],d[k+(<<(i-))][l][i-][j]);
else d[k][l][i][j]=max(d[k][l][i-][j-],max(d[k][l+(<<(j-))][i-][j-],
max(d[k+(<<(i-))][l][i-][j-],d[k+(<<(i-))][l+(<<(j-))][i-][j-])));
}
}
}
}
} int query(int lx,int ly,int rx,int ry)
{
int ri=floor(log(rx-lx+1.0)/log(2.0)+0.000001);
int ci=floor(log(ry-ly+1.0)/log(2.0)+0.000001);
int temp=d[lx][ly][ri][ci];
temp=max(temp,d[lx][ry-(<<ci)+][ri][ci]);
temp=max(temp,d[rx-(<<ri)+][ly][ri][ci]);
temp=max(temp,d[rx-(<<ri)+][ry-(<<ci)+][ri][ci]);
if(temp==A[lx][ly] || temp==A[lx][ry] ||
temp==A[rx][ly] || temp==A[rx][ry])
flag=;
return temp;
} int main()
{
int n,m,i,j,q,lx,ly,rx,ry,ans;
while(~scanf("%d %d",&n,&m))
{
for(i=;i<n;i++)
for(j=;j<m;j++) scanf("%d",&A[i][j]);
RMQ_init(n,m);
scanf("%d",&q);
while(q--)
{
scanf("%d %d %d %d",&lx,&ly,&rx,&ry);
lx--;ly--;rx--,ry--;
flag=;
ans=query(lx,ly,rx,ry);
printf("%d ",ans);
printf(flag?"yes\n":"no\n");
}
}
return ;
}