zoukankan      html  css  js  c++  java
  • hdu2888 二维RMQ

    Check Corners

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


    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
     
    求子矩阵内最大的值是多少。
    思路:
    二维RMQ处理。
    dp[row][col][i][j] 表示[row,row+2^i-1]x[col,col+2^j-1] 二维区间内的最小值
    =  max{dp[row][col][i][j-1],dp[row][col][i-1][j],dp[row][col+2^(j-1)][i][j-1],dp[row+2^(i-1)][col][i-1][j]}
     
    查询结果为
          max{dp[sx][sy][kx][ky],dp[sx][ey-2^ky+1][kx][ky],dp[ex-2^kx+1][sy][kx][ky],dp[ex-2^kx+1][ey-2^ky+1][kx][ky]}
     
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1000000001
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    //#define pi acos(-1.0)
    using namespace std;
    const int MAXN = 301;
    int a[MAXN][MAXN],n,m,dp[MAXN][MAXN][9][9];
    void Init()
    {
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                dp[i][j][0][0] = a[i][j];
            }
        }
        for(int pi = 0; pi < 10; pi++){
            for(int pj = 0; pj < 10; pj++){
                if(pi == 0 && pj == 0)continue;
                for(int i = 1; i <= n; i++){
                    for(int j = 1; j <= m; j++){
                        if(i + (1 << pi) - 1 > n || j + (1 << pj) - 1 > m)continue;
                        if(pi == 0){
                            dp[i][j][pi][pj] = max(dp[i][j][pi][pj-1],dp[i][j+(1<<(pj-1))][pi][pj-1]);
                        }
                        else {
                            dp[i][j][pi][pj] = max(dp[i][j][pi-1][pj],dp[i+(1<<(pi-1))][j][pi-1][pj]);
                        }
                    }
                }
            }
        }
    }
    void getans(int x1,int y1,int x2,int y2)
    {
        int kx,ky;
        kx = (int)(log((double)(x2 - x1)) / log(2.0));
        ky = (int)(log((double)(y2 - y1)) / log(2.0));
        int ans = -INF;
        ans = max(ans,dp[x1][y1][kx][ky]);
        ans = max(ans,dp[x2 - (1 << kx) + 1][y1][kx][ky]);
        ans = max(ans,dp[x1][y2 - (1 << ky) + 1][kx][ky]);
        ans = max(ans,dp[x2 - (1 << kx) + 1][y2 - (1 << ky) + 1][kx][ky]);
        printf("%d ",ans);
        if(a[x1][y1] == ans || a[x1][y2] == ans || a[x2][y1] == ans || a[x2][y2] == ans)printf("yes
    ");
        else printf("no
    ");
    }
    void solve()
    {
        int q;
        scanf("%d",&q);
        int x1,y1,x2,y2;
        while(q--){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            getans(x1,y1,x2,y2);
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m)){
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= m; j++){
                    scanf("%d",&a[i][j]);
                }
            }
            Init();
            solve();
        }
        return 0;
    }
  • 相关阅读:
    从xml中改变checkBox大小和形状
    Android 数字签名学习笔记
    Android 说说钟表控件
    Android中选项卡TabHost的基本使用
    Android 实现图片反转
    Android openfire插件开发
    最大递增子序列——[Usaco2006 Oct]Hungry Cows饥饿的奶牛
    矩阵二分乘法(可做模板)——hdu1575
    树形递归——1621: [Usaco2008 Open]Roads Around The Farm
    规律题——Codeforces Beta Round #11 B
  • 原文地址:https://www.cnblogs.com/sweat123/p/5539646.html
Copyright © 2011-2022 走看看