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 #include <vector>
 9 #include <cstdio>
10 #include <cctype>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 #define INF 0x3f3f3f3f
17 #define inf (-((LL)1<<40))
18 #define lson k<<1, L, (L + R)>>1
19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
20 #define mem0(a) memset(a,0,sizeof(a))
21 #define mem1(a) memset(a,-1,sizeof(a))
22 #define mem(a, b) memset(a, b, sizeof(a))
23 #define FIN freopen("in.txt", "r", stdin)
24 #define FOUT freopen("out.txt", "w", stdout)
25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
26 #define dec(i, a, b) for(int i = a; i >= b; i --)
27 
28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
32 
33 //typedef __int64 LL;
34 typedef long long LL;
35 const int MAXN = 50000 + 100;
36 const int MAXM = 110000;
37 const double eps = 1e-8;
38 LL MOD = 1000000007;
39 
40 int m, n;
41 int mx[302][9][302][9];
42 int idx[302], q, lx, ly, rx, ry;
43 
44 void rmq_init(int m, int n) {
45     for(int i = 0; (1<<i) <= m; i ++) {
46         for(int j = 0; (1<<j) <= n; j ++) {
47             if(i == 0 && j == 0) continue;
48             int len2 = (1 << j), len1 = (1 << i);
49             for(int x = 1; x + len1 - 1 <= m; x ++) {
50                 for(int y = 1; y + len2 - 1 <= n; y ++) {
51                     if(i == 0) mx[x][i][y][j] = max(mx[x][i][y][j - 1], mx[x][i][y + (len2 >> 1)][j - 1]);
52                     else mx[x][i][y][j] = max(mx[x][i - 1][y][j], mx[x + (len1 >> 1)][i - 1][y][j]);
53                 }
54             }
55         }
56     }
57     for(int i = 1; i <= m || i <= n; i ++) {
58         idx[i] = 0;
59         while((1 << (idx[i] + 1)) <= i) idx[i] ++;
60     }
61 }
62 
63 int rmq(int lx, int rx, int ly, int ry) {
64     int a = idx[rx - lx + 1], la = (1 << a);
65     int b = idx[ry - ly + 1], lb = (1 << b);
66     return max(max(max(mx[lx][a][ly][b],
67                        mx[rx - la + 1][a][ly][b]),
68                        mx[lx][a][ry - lb + 1][b]),
69                        mx[rx - la + 1][a][ry - lb + 1][b]);
70 }
71 
72 int main()
73 {
74     //FIN;
75     while(~scanf("%d %d", &m, &n)) {
76         mem0(mx);
77         rep (i, 1, m) rep (j, 1, n)
78             scanf("%d", &mx[i][0][j][0]);
79         rmq_init(m, n);
80         scanf("%d", &q);
81         while(q--) {
82             scanf("%d %d %d %d", &lx, &ly, &rx, &ry);
83             int ma = rmq(lx, rx, ly, ry);
84             printf("%d %s\n", ma, ma == mx[lx][0][ly][0] || ma == mx[lx][0][ry][0]
85                                || ma == mx[rx][0][ly][0] || ma == mx[rx][0][ry][0]
86                                ? "yes" : "no");
87         }
88     }
89     return 0;
90 }

 

posted @ 2015-07-20 20:51  再见~雨泉  阅读(216)  评论(0编辑  收藏  举报