hdu 2888 Check Corners(RMQ)

题目链接:hdu 2888 Check Corners

题目大意:给定一个矩阵,每次查询矩阵中的最大值,并且判断该最大值是否在所查询的角落上。

解题思路:一开始用线段树,一维RMQ都超时了,然后换成了二维的RMQ,结果MLE,dp数组换成9?9就过了。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 305;

int N, M, Q, g[maxn][maxn], dp[maxn][maxn][9][9];

void rmq_init(int n, int m) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
            dp[i][j][0][0] = g[i][j];
    }

    for (int x = 0; (1<<x) <= n; x++)
        for (int y = 0; (1<<y) <= m; y++)
            if (x + y)
                for (int i = 1; i + (1<<x) - 1 <= n; i++)
                    for (int j = 1; j + (1<<y) - 1 <= m; j++) {
                        if (x)
                            dp[i][j][x][y] = max(dp[i][j][x-1][y], dp[i+(1<<(x-1))][j][x-1][y]);
                        else
                            dp[i][j][x][y] = max(dp[i][j][x][y-1], dp[i][j+(1<<(y-1))][x][y-1]);
                    }
}

int rmq_query(int x1, int y1, int x2, int y2) {
    int x = 0, y = 0;
    while ((1<<(x+1)) <= x2 - x1 + 1) x++;
    while ((1<<(y+1)) <= y2 - y1 + 1) y++;
    x2 = x2 - (1<<x) + 1;
    y2 = y2 - (1<<y) + 1;

    return max( max(dp[x1][y1][x][y], dp[x2][y1][x][y]), max(dp[x1][y2][x][y], dp[x2][y2][x][y]));
}

int main () {
    while (scanf("%d%d", &N, &M) == 2) {
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++)
                scanf("%d", &g[i][j]);
        }
        rmq_init(N, M);

        scanf("%d", &Q);
        int x1, y1, x2, y2;
        while (Q--) {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            int ans = rmq_query(x1, y1, x2, y2);
            bool flag = false;
            if (ans == g[x1][y1] || ans == g[x1][y2] || ans == g[x2][y1] || ans == g[x2][y2])
                flag = true;
            printf("%d %s\n", ans, flag ? "yes" : "no");
        }
    }
    return 0;
}
时间: 2024-11-05 09:20:20

hdu 2888 Check Corners(RMQ)的相关文章

hdu 2888 Check Corners

/* 题意:给出m,n代表给出长为m宽为n的矩阵,然后给出一个q代表查询的次数,然后q行每行给出一个 r1, c1, r2, c2 且满足(1 <= r1 <= r2 <= m, 1 <= c1 <= c2 <= n) 输出这个矩形范围内的最大值,如果最大值是四个顶点某一个的话就输出yes否则输出no */ #include<stdio.h> #include<math.h> #define Max(a,b) a>b?a:b int dp[

HDU 2888:Check Corners 二维RMQ

Check Corners 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意: 求矩形区间内的最大值 题解: 二维RMQ,和一维的区别不大,按一维的做法求出每一行上的RMQ,再处理行与行之间的关系就好了. 代码 #include<stdio.h> const int N=301; const int M=301; int dp[N][M][9][9]; int mmax(int x,int y) { return x>y?x:

【HDOJ 2888】Check Corners(裸二维RMQ)

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 tha

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 ma

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

HDU 2888:Check Corners(二维RMQ)

http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中的最大值,和判断四个角有没有等于这个最大值的. 思路:二维RMQ模板题.注意内存卡的挺紧的. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5

Check Corners HDU - 2888(二维RMQ)

就是板题.. 查询子矩阵中最大的元素...然后看看是不是四个角落的  是就是yes  不是就是no  判断一下就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #includ

HDU2888 Check Corners(二维RMQ)

有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ 1 #pragma comment(linker, "/STACK:1677721600") 2 #include <map> 3 #include <set> 4 #include <stack> 5 #include <queue> 6 #include <cmath> 7 #include <ctime> 8

hdu 5289 Assignment 二分+rmq

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 169 Problem Description Tom owns a company and he is