HDU2888 Check Corners
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
Source
2009 Multi-University Training Contest 9 - Host by HIT
二维RMQ练习题。
一维RMQ二分维护一个区间,而二维RMQ通过维护四个等分的矩形区间维护了一个区间的最值,基本原理差不多
理解的还不是很透彻,代码基本靠抄。
这题内存限制范围很小,数组稍微开大就MLE
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 int f[302][302][9][9]; 9 //f[i][j][x][y]记录以(i,j)为左上角,(i+(1<<x),j+(1<<y))为右下角的矩形内的最大值 10 int mp[302][302]; 11 int n,m; 12 void init(){ 13 int i,j; 14 int k,l; 15 for(i=1;i<=n;i++) 16 for(j=1;j<=m;j++) 17 f[i][j][0][0]=mp[i][j]; 18 int kn=(int)(log((double)n)/log(2.0)); 19 int km=(int)(log((double)m)/log(2.0)); 20 for(i=0;i<=kn;i++) 21 for(j=0;j<=km;j++){ 22 if(i==0 && j==0)continue; 23 for(k=1;k+(1<<i)-1<=n;k++) 24 for(l=1;l+(1<<j)-1<=m;l++){ 25 if(!i)//i==0 && j!=0 26 f[k][l][i][j]=max(f[k][l][i][j-1],f[k][l+(1<<(j-1))][i][j-1]); 27 else //i!=0 28 f[k][l][i][j]=max(f[k][l][i-1][j],f[k+(1<<(i-1))][l][i-1][j]); 29 } 30 } 31 return; 32 } 33 int RMQ(int x1,int y1,int x2,int y2){ 34 int kn=(int)(log(double(x2-x1+1))/log(2.0)); 35 int km=(int)(log(double(y2-y1+1))/log(2.0)); 36 int a=max(f[x1][y1][kn][km],f[x2-(1<<kn)+1][y1][kn][km]); 37 int b=max(f[x1][y2-(1<<km)+1][kn][km],f[x2-(1<<kn)+1][y2-(1<<km)+1][kn][km]); 38 return max(a,b); 39 } 40 int main(){ 41 int i,j; 42 int k; 43 while(scanf("%d%d",&n,&m)!=-1){ 44 45 for(i=1;i<=n;i++) 46 for(j=1;j<=m;j++) 47 scanf("%d",&mp[i][j]); 48 init(); 49 int x1,x2,y1,y2; 50 scanf("%d",&k); 51 while(k--){ 52 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 53 int ans=RMQ(x1,y1,x2,y2); 54 printf("%d ",ans); 55 if(ans==mp[x1][y1] || ans==mp[x2][y2] || ans==mp[x1][y2] || ans==mp[x2][y1]) 56 puts("yes"); 57 else puts("no"); 58 } 59 } 60 return 0; 61 }
本文为博主原创文章,转载请注明出处。