现在的位置: 首页 > 综合 > 正文

HDU 2888 Check Corners 二维RMQ

2018年01月20日 ⁄ 综合 ⁄ 共 3424字 ⁄ 字号 评论关闭

Check Corners

Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1923    Accepted Submission(s): 683

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
/*
HDOJ 2888 二维RMQ

	给一个矩阵,然后给Q个询问,每个询问有四个数,分别代表询问的子矩阵的左上角和右下角,
然后找出子矩阵的最大值输出,然后再把这个值与子矩阵的四个角的值比较,
如果有至少一个等于这个最大值就输出“yes”,否则输出“no”。 

一维:f(i, j)=[i, i+2^j - 1]可以由f(i, j - 1)和f(i+2^(j-1), j-1)导出
dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); 
查询:max(dp[a][k],dp[b-(1<<k)+1][k]); 
 
dp[row][col][i][j] 表示行从row->row+2^i-1 列从col->col+2^j-1二维区间里最大值
dp[row][col][i][j]= max{dp[row][col][i-1][j],dp[row+2^(i-1)][col][i-1][j],
dp[row][col][i][j-1],dp[row][col+2^(j-1)][i][j-1]}

查询RMQ rmq(int sx,int ex,int sy,int ey)
同一维的将sx->ex分为两个2^kx区间 将sy->ey分为两个2^ky的区间
kx=(int)log2(ex-sx+1) ky=(int)log2(ey-sy+1)
查询结果为
max{dp[sx][sy][kx][ky],dp[ex-2^kx+1][sy][kx][ky],
	dp[sx][ey-2^ky+1][kx][ky],dp[ex-2^kx+1][ey-2^ky+1][kx][ky]}
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
#define N 303

int dp[N][N][9][9];//最大值 
int a[N][N],n,m;


void init()
{
	int i,j,limit,limit2,k1,k2,r,c;
	memset(dp,0,sizeof(dp));
	
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			dp[i][j][0][0]=a[i][j];
	

	k1=(int)(log(double(n))/log(2.0));
	k2=(int)(log(double(m))/log(2.0));
	
	
	for(i=0;i<=k1;i++)
	{
		for(j=0;j<=k2;j++)
		{
			if(i==0&&j==0) continue;
			limit=n+1-(1<<i);
			limit2=m+1-(1<<j);
			
			for(r=1;r<=limit;r++)
			{
				for(c=1;c<=limit2;c++) 
				{
					if(i==0)
						dp[r][c][i][j]=max(dp[r][c][i][j-1],dp[r][c+(1<<(j-1))][i][j-1]);
					 else  
                        dp[r][c][i][j]=max(dp[r][c][i-1][j],dp[r+(1<<(i-1))][c][i-1][j]); 
				} 
			}
				
		}
	}	
}

int query(int r1, int c1, int r2, int c2)  
{  
    int kr=(int)(log(double(r2-r1+1))/log(2.0));  
    int kc=(int)(log(double(c2-c1+1))/log(2.0));  
  
    int t1=dp[r1][c1][kr][kc];  
    int t2=dp[r2-(1<<kr)+1][c1][kr][kc];  
    int t3=dp[r1][c2-(1<<kc)+1][kr][kc];  
    int t4=dp[r2-(1<<kr)+1][c2-(1<<kc)+1][kr][kc];  
  
    return max(max(t1,t2),max(t3,t4));  
}  


int main()
{
	int k,i,j,r1,r2,c1,c2,ans;
	
	//freopen("test.txt","r",stdin);
	while(scanf("%d%d",&n,&m)!=EOF)
	{	
		for(i=1;i<=n;i++)
			for(j=1;j<=m;j++)
				scanf("%d",&a[i][j]);
			
		init();
		scanf("%d",&k);
		
		while(k--)
		{
			scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
			ans=query(r1,c1,r2,c2);
			printf("%d ",ans);
			if(a[r1][c1]==ans||a[r1][c2]==ans||a[r2][c1]==ans||a[r2][c2]==ans)
				printf("yes\n");  
            else    
				printf("no\n");   
		} 
	}
	return 0;
} 

抱歉!评论已关闭.