HDU-OJ-2888 Check Corners

69 篇文章 0 订阅
3 篇文章 0 订阅

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. 
 

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
 

————————————————————清晨的分割线————————————————————
思路:一日之计在于晨。这道二维的RMQ我终于用二维的ST算法撸出来了。唉,这就是天资差的人。必须用更多时间来弥补。这道题是比较卡内存的,除此之外,多组测试,每组测试的询问次数1000000,枚举可是万万不能的。一开始我就用了一维ST+枚举,10s都超时了。方法是这样的:直接平行推广。从以前的d[N][i]变成d[N][N][i][j]。然后,以前是用左起2^k个和右起2^k个来覆盖查询区间,现在是左上、右上、左下、右下来覆盖。初始化也是这个道理。
但是你会WA。因为有一种特殊情况你没有考虑到。在一维的时候,初始化的核心是这样的:
    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开始。
P.S.代码写太丑,所以用了宏。LEK、REK代表列上的“右起2^k个”和行上的“下起2^k个”。
代码如下:
/****************************************/
 #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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值