【二维RMQ】Check Corners

Check Corners

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


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
 
题目大意:
  输入N,M表示有N行M列,然后输入这个矩阵的值。接下来输入一个Q,表示询问的次数,接下来有Q行,每一行输入四个数值a,b,c,d表示子矩阵的对角点。求出子矩阵(a,b,c,d)区域中的最大值Max,如果这个最大值存在于子矩阵的四个角,则输出yes,否则输出no。
 
解法:一维RMQ的升级,转化成二维RMQ即可、
  在理解了ST算法和一维RMQ的思想用,转化成二维的RMQ并不难,反而,倒是题目有许多细节需要注意、
  1,输入的a,b,c,d,不一定就算a<c,b<d。
  2,设置二维RMQ的时候,横向和纵向的长度初始值需要从0开始,而不是从1开始,因为可能是单行或者单列。
  3,设置RMQ的记录数组的大小只需要开到9,因为2^8<300<2^9。
  4,ST算法中取最值,需要注意判断k=0和l=0的情况、
代码:(2015.8.13,再从新学了一维RMQ后,发现二维也会写了~)
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #define max(a,b) (a)>(b)?(a):(b)
 5 #define min(a,b) (a)<(b)?(a):(b)
 6 #define MAX 302
 7 using namespace std;
 8 int maxsum[MAX][MAX][9][9];
 9 int Num[MAX][MAX];
10 
11 void Cread_ST(int N,int M)
12 {
13     for(int i=1;i<=N;i++)
14     {
15         for(int j=1;j<=M;j++)
16             maxsum[i][j][0][0]=Num[i][j];
17     }
18     int Lenk=(int)(log(N)/log(2.0));
19     int Lenl=(int)(log(M)/log(2.0));
20     for(int k=0;k<=Lenk;k++)
21     {
22         for(int l=0;l<=Lenl;l++)
23         {
24             for(int i=1;i+(1<<k)-1<=N;i++)
25             {
26                 for(int j=1;j+(1<<l)-1<=M;j++)
27                 {
28                     int TMD=i+(1<<(k-1));
29                     int TMP=j+(1<<(l-1));
30                     if(!(l+k))continue;
31                     if(!k)
32                     {maxsum[i][j][k][l]=max(maxsum[i][j][k][l-1],maxsum[i][TMP][k][l-1]);continue;}
33                     if(!l)
34                     {maxsum[i][j][k][l]=max(maxsum[i][j][k-1][l],maxsum[TMD][j][k-1][l]);continue;}
35                     else
36                     {
37                         maxsum[i][j][k][l]=max(maxsum[i][j][k-1][l-1],maxsum[TMD][j][k-1][l-1]);
38                         maxsum[i][j][k][l]=max(maxsum[i][j][k][l],maxsum[i][TMP][k-1][l-1]);
39                         maxsum[i][j][k][l]=max(maxsum[i][j][k][l],maxsum[TMD][TMP][k-1][l-1]);
40                     }
41                 }
42             }
43         }
44     }
45 }
46 int RMQ(int l1,int r1,int l2,int r2)/*需要保证l1<=l2,r1<=r2*/
47 {
48     int kx,ky,TMD,TMP,Max,Min;
49     kx=(int)(log(l2-l1+1.0)/log(2.0));
50     ky=(int)(log(r2-r1+1.0)/log(2.0));
51     TMD=l2-(1<<kx)+1;
52     TMP=r2-(1<<ky)+1;
53     Max=max(maxsum[l1][r1][kx][ky],maxsum[TMD][TMP][kx][ky]);
54     Max=max(Max,maxsum[TMD][r1][kx][ky]);
55     Max=max(Max,maxsum[l1][TMP][kx][ky]);
56     return Max;
57 }
58 int main()
59 {
60     int N,M,i,j,Q,a,b,c,d,k,Max,TMP;
61     while(scanf("%d%d",&N,&M)!=EOF)
62     {
63         for(i=1;i<=N;i++)
64         {
65             for(j=1;j<=M;j++)
66                 scanf("%d",&Num[i][j]);
67         }
68         Cread_ST(N,M);
69         scanf("%d",&Q);
70         while(Q--)
71         {
72             scanf("%d %d %d %d",&a,&b,&c,&d);
73             if(a>c){TMP=c;c=a;a=TMP;};/*判断a,c*/
74             if(b>d){TMP=d;d=b;b=TMP;};/*判断b,d*/
75             Max=RMQ(a,b,c,d);
76             printf("%d ",Max);
77             if(Max==Num[a][b]||Max==Num[c][d]||Max==Num[a][d]||Max==Num[c][b])
78                 printf("yes\n");
79             else printf("no\n");
80         }
81     }
82     return 0;
83 }
View Code

 

posted @ 2015-08-14 19:01  Wurq  阅读(267)  评论(0编辑  收藏  举报