Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2513 Accepted Submission(s): 904
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<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
//#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int a[MAXN][MAXN],n,m,dp[MAXN][MAXN][][];
void Init()
{
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
dp[i][j][][] = a[i][j];
}
}
for(int pi = ; pi < ; pi++){
for(int pj = ; pj < ; pj++){
if(pi == && pj == )continue;
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
if(i + ( << pi) - > n || j + ( << pj) - > m)continue;
if(pi == ){
dp[i][j][pi][pj] = max(dp[i][j][pi][pj-],dp[i][j+(<<(pj-))][pi][pj-]);
}
else {
dp[i][j][pi][pj] = max(dp[i][j][pi-][pj],dp[i+(<<(pi-))][j][pi-][pj]);
}
}
}
}
}
}
void getans(int x1,int y1,int x2,int y2)
{
int kx,ky;
kx = (int)(log((double)(x2 - x1)) / log(2.0));
ky = (int)(log((double)(y2 - y1)) / log(2.0));
int ans = -INF;
ans = max(ans,dp[x1][y1][kx][ky]);
ans = max(ans,dp[x2 - ( << kx) + ][y1][kx][ky]);
ans = max(ans,dp[x1][y2 - ( << ky) + ][kx][ky]);
ans = max(ans,dp[x2 - ( << kx) + ][y2 - ( << ky) + ][kx][ky]);
printf("%d ",ans);
if(a[x1][y1] == ans || a[x1][y2] == ans || a[x2][y1] == ans || a[x2][y2] == ans)printf("yes\n");
else printf("no\n");
}
void solve()
{
int q;
scanf("%d",&q);
int x1,y1,x2,y2;
while(q--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
getans(x1,y1,x2,y2);
}
}
int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
scanf("%d",&a[i][j]);
}
}
Init();
solve();
}
return ;
}