Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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”.)
Input
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.
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.
Output
For each test case, print Q lines with two numbers on each line, the required maximum integer and the result of the “Check corners” using “yes” or “no”. Separate the two parts with a single space.
Sample Input
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
Sample Output
20 no 13 no 20 yes 4 yes
for(int j = 1; (1<<j) <= n; j++)
for(int i = 0; i + (1<<j) - 1 < n; i++)
d[i][j] = max(d[i][j-1], d[i+(1<<(j-1))][j-1]);
注意到,j是从1开始的。(2^1个数开始)那么就要想一想,为什么?不清楚代码的任何一个细节,拿过来用都会给你带来血的代价。因为在一维的时候,单个数自己的最值是没有意义的。因此省掉了这一层循环,这是个优化。可是一旦平行推广,单个数变成了单行、单列,还没有意义吗?明显是有意义的。一列数的最值不保证是第一行的那个。对,都要从0开始,亦即从2^0开始。
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
#define REK r_e-(1<<ki)+1
#define LEK l_e-(1<<kj)+1
#define JK1 j+(1<<(kj-1))
#define IK1 i+(1<<(ki-1))
const int N = 305;
int n, m;
int a[N][N], d[N][N][9][9];
void R_init()
{
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
d[i][j][0][0] = a[i][j];//递推的起始条件
for(int ki = 0; (1<<ki) <= n; ki++)
for(int kj = 0; (1<<kj) <= m; kj++)
if(ki + kj)//都是0就是单个数了,没有意义
for(int i = 0; i + (1<<ki) - 1 < n; i++)
for(int j = 0; j + (1<<kj) - 1 < m; j++) {
if(!ki) {//单行
d[i][j][ki][kj] = max(d[i][j][ki][kj-1], d[i][JK1][ki][kj-1]);
}
else if(!kj) {//单列
d[i][j][ki][kj] = max(d[i][j][ki-1][kj], d[IK1][j][ki-1][kj]);
}
else {
int u1 = max(d[i][j][ki-1][kj-1], d[i][JK1][ki-1][kj-1]);
int u2 = max(d[IK1][j][ki-1][kj-1], d[IK1][JK1][ki-1][kj-1]);
d[i][j][ki][kj] = max(u1, u2);
}
}
}
int R_quer(int r_s, int l_s, int r_e, int l_e)
{
int ki = 0, kj = 0;
ki = log(1.0 * (r_e-r_s+1)) / log(2.0);
kj = log(1.0 * (l_e-l_s+1)) / log(2.0);//找到符合条件的ki、kj,分别是行上的覆盖、列上的覆盖
int u1 = max(d[r_s][l_s][ki][kj], d[r_s][LEK][ki][kj]);
int u2 = max(d[REK][l_s][ki][kj], d[REK][LEK][ki][kj]);
return max(u1, u2);
}
int main()
{
int op;
while(~scanf("%d%d", &n, &m)) {
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%d", &a[i][j]);
R_init();
scanf("%d", &op);
while(op--) {
int r_s, l_s, r_e, l_e;
int ans, flag = 0;
scanf("%d%d%d%d", &r_s, &l_s, &r_e, &l_e);
r_s--; r_e--; l_s--; l_e--;
ans = R_quer(r_s, l_s, r_e, l_e);
printf("%d ", ans);
if(a[r_s][l_s] == ans || a[r_s][l_e] == ans || a[r_e][l_e] == ans || a[r_e][l_s] == ans)
flag = 1;
printf("%s\n", flag ? "yes" : "no");
}
}
return 0;
}