zoukankan      html  css  js  c++  java
  • hdu 2888 二维RMQ

                                          Check Corners

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


    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的正整数矩阵,Q条询问:给一个子矩阵的左上角跟右下角的坐标,求这个矩阵的元素最大值,若最大值与四个顶点的值有一个相等,输出max yes,否则输出max no。
     
    分析:用二维RMQ离线处理,O(1)查询,手贱的把数组多开大了4跟1就超内存了。。。。。。这游戏真难。
    
    
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    
    const int maxn=301;
    const int maxm=9;
    
    int    A[maxn][maxn],flag;
    int d[maxn][maxn][maxm][maxm];
    
    inline int max(int a,int b){ return a>b?a:b;}
    
    void RMQ_init(int n,int m)
    {
        int i,j,k,l;
        for(i=0;i<n;i++)
        for(j=0;j<m;j++) d[i][j][0][0]=A[i][j];
        for(i=0;(1<<i)<=n;i++)
        {
            for(j=0;(1<<j)<=m;j++)
            {
                if(i==0 && j==0) continue;
                for(k=0;k+(1<<i)-1<n;k++)
                {
                    for(l=0;l+(1<<j)-1<m;l++)
                    {
                        if(i==0 && j!=0) d[k][l][i][j]=max(d[k][l][i][j-1],d[k][l+(1<<(j-1))][i][j-1]);
                        else if(i!=0 && j==0) d[k][l][i][j]=max(d[k][l][i-1][j],d[k+(1<<(i-1))][l][i-1][j]);
                        else d[k][l][i][j]=max(d[k][l][i-1][j-1],max(d[k][l+(1<<(j-1))][i-1][j-1],
                            max(d[k+(1<<(i-1))][l][i-1][j-1],d[k+(1<<(i-1))][l+(1<<(j-1))][i-1][j-1])));
                    }
                }
            }
        }
    }
    
    int query(int lx,int ly,int rx,int ry)
    {
        int ri=floor(log(rx-lx+1.0)/log(2.0)+0.000001);
        int ci=floor(log(ry-ly+1.0)/log(2.0)+0.000001);
        int temp=d[lx][ly][ri][ci];
        temp=max(temp,d[lx][ry-(1<<ci)+1][ri][ci]);
        temp=max(temp,d[rx-(1<<ri)+1][ly][ri][ci]);
        temp=max(temp,d[rx-(1<<ri)+1][ry-(1<<ci)+1][ri][ci]);
        if(temp==A[lx][ly] || temp==A[lx][ry] ||
            temp==A[rx][ly] || temp==A[rx][ry])
            flag=1;
        return temp;
    }
    
    int main()
    {
        int n,m,i,j,q,lx,ly,rx,ry,ans;
        while(~scanf("%d %d",&n,&m))
        {
            for(i=0;i<n;i++)
            for(j=0;j<m;j++) scanf("%d",&A[i][j]);
            RMQ_init(n,m);
            scanf("%d",&q);
            while(q--)
            {
                scanf("%d %d %d %d",&lx,&ly,&rx,&ry);
                lx--;ly--;rx--,ry--;
                flag=0;
                ans=query(lx,ly,rx,ry);
                printf("%d ",ans);
                printf(flag?"yes
    ":"no
    ");
            }
        }
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    Myeclipse学习总结(5)——Myeclipse常用快捷键再学习
    Myeclipse学习总结(5)——Myeclipse常用快捷键再学习
    Java基础学习总结(37)——Java23中设计模式(Design Patterns)详解
    Java基础学习总结(37)——Java23中设计模式(Design Patterns)详解
    Java基础学习总结(37)——Java23中设计模式(Design Patterns)详解
    Git学习总结(4)——我的Git忽略文件
    Git学习总结(4)——我的Git忽略文件
    Git学习总结(4)——我的Git忽略文件
    《跟唐老师学习云网络》 -第5篇 Ping喂报文
    深入比特币原理(五)——高级交易与脚本
  • 原文地址:https://www.cnblogs.com/xiong-/p/3586546.html
Copyright © 2011-2022 走看看