hdu 2888(二维RMQ)

来源:互联网 发布:linux系统下计算派的值 编辑:程序博客网 时间:2024/05/11 04:29

Check Corners

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


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 44 4 10 72 13 9 115 7 8 2013 20 8 241 1 4 41 1 3 31 3 3 41 1 1 1
 

Sample Output
20 no13 no20 yes4 yes
 

Source
2009 Multi-University Training Contest 9 - Host by HIT
 

Recommend
gaojie
 
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2888
分析:二维的RMQ问题,没有多大变化。。。
代码:
#include<cstdio>#define max(a,b) (a>b?a:b)#define mm 303int f[mm][mm][9][9],s[mm][mm];int n,m;int log2(int x){    int k=0;    while((1<<(k+1))<x)++k;    return k;}void ST(){    int i,j,u,v,logn=log2(n),logm=log2(m);    for(u=0;u<=logn;++u)        for(v=0;v<=logm;++v)            if(u+v)for(i=1;i+(1<<u)-1<=n;++i)                for(j=1;j+(1<<v)-1<=m;++j)                if(v==0)f[i][j][u][v]=max(f[i][j][u-1][v],f[i+(1<<(u-1))][j][u-1][v]);                else f[i][j][u][v]=max(f[i][j][u][v-1],f[i][j+(1<<(v-1))][u][v-1]);}int get(int r1,int c1,int r2,int c2){    int k=log2(r2-r1+1),t=log2(c2-c1+1);    int a=max(f[r1][c1][k][t],f[r1][c2-(1<<t)+1][k][t]);    int b=max(f[r2-(1<<k)+1][c1][k][t],f[r2-(1<<k)+1][c2-(1<<t)+1][k][t]);    return max(a,b);}void in(int &a){    char c;    while((c=getchar())<'0'||c>'9');    for(a=0;c>='0'&&c<='9';c=getchar())a=a*10+c-'0';}void out(int x){    if(x>9)out(x/10);    putchar(x%10+48);}int main(){    int i,j,k,q,r1,r2,c1,c2;    while(scanf("%d%d",&n,&m)!=-1)    {        for(i=1;i<=n;++i)            for(j=1;j<=m;++j)in(s[i][j]),f[i][j][0][0]=s[i][j];        ST();        in(q);        while(q--)        {            in(r1),in(c1),in(r2),in(c2);            k=get(r1,c1,r2,c2);            out(k);            if(s[r1][c1]==k||s[r2][c2]==k||s[r1][c2]==k||s[r2][c1]==k)puts(" yes");            else puts(" no");        }    }    return 0;}


原创粉丝点击