• [hdu2888]二维RMQ


    题意:求矩形内最大值。二维RMQ。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 
     17 using namespace std;
     18 
     19 #define mem0(a) memset(a, 0, sizeof(a))
     20 #define lson l, m, rt << 1
     21 #define rson m + 1, r, rt << 1 | 1
     22 #define define_m int m = (l + r) >> 1
     23 #define Rep(a, b) for(int a = 0; a < b; a++)
     24 #define lowbit(x) ((x) & (-(x)))
     25 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     26 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     27 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     28 
     29 typedef double db;
     30 typedef long long LL;
     31 typedef pair<int, int> pii;
     32 typedef multiset<int> msi;
     33 typedef multiset<int>::iterator msii;
     34 typedef set<int> si;
     35 typedef set<int>::iterator sii;
     36 typedef vector<int> vi;
     37 
     38 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
     39 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
     40 const int maxn = 1e5 + 7;
     41 const int maxm = 1e5 + 7;
     42 const int maxv = 1e7 + 7;
     43 const int MD = 1e9 +7;
     44 const int INF = 1e9 + 7;
     45 const double PI = acos(-1.0);
     46 const double eps = 1e-10;
     47 
     48 int a[300][300], t[301], f[300][9][300][9], n, m;
     49 
     50 void Init_RMQ() {
     51     for (int i = 0; i < n; i++) {
     52         for (int j = 0; j < m; j++) {
     53             f[i][0][j][0] = a[i][j];
     54         }
     55     }
     56     for (int dx = 0; dx <= 8; dx++) {
     57         for (int dy = 0; dy <= 8; dy++) {
     58             if (dx == 0 && dy == 0) continue;
     59             for (int i = 0; i + (1 << dx) <= n; i++) {
     60                 for (int j = 0; j + (1 << dy) <= m; j++) {
     61                    int &p = f[i][dx][j][dy];
     62                     if (dx) p = max(f[i][dx - 1][j][dy], f[i + (1 << (dx - 1))][dx - 1][j][dy]);
     63                     else p = max(f[i][dx][j][dy - 1], f[i][dx][j + (1 << (dy - 1))][dy - 1]);
     64                 }
     65             }
     66         }
     67     }
     68 }
     69 
     70 int RMQ(int x1, int y1, int x2, int y2) {
     71     int l1 = t[x2 - x1 + 1], l2 = t[y2 - y1 + 1];
     72     return max(f[x1][l1][y1][l2], max(f[x1][l1][y2 - (1 << l2) + 1][l2],
     73             max(f[x2 - (1 << l1) + 1][l1][y1][l2], f[x2 - (1 << l1) + 1][l1][y2 - (1 << l2) + 1][l2])));
     74 }
     75 
     76 int main() {
     77     //freopen("in.txt", "r", stdin);
     78     for (int i = 1; i <= 300; i++) {
     79         int j = 0;
     80         while ((1 << (j + 1)) <= i) j++;
     81         t[i] = j;
     82     }
     83     int x1, x2, y1, y2, q;
     84     while (cin >> n >> m) {
     85         for (int i = 0; i < n; i++) {
     86             for (int j = 0; j < m; j++) {
     87                 scanf("%d", &a[i][j]);
     88             }
     89         }
     90         Init_RMQ();
     91         cin >> q;
     92         for (int i = 0; i < q; i++) {
     93             scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
     94             x1--; x2--; y1--; y2--;
     95             int res = RMQ(x1, y1, x2, y2);
     96             printf("%d ", res);
     97             if (a[x1][y1] == res || a[x2][y1] == res || a[x1][y2] == res || a[x2][y2] == res) puts("yes");
     98             else puts("no");
     99         }
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    字符串题表
    插头dp题表
    点分治题表
    Kd-tree题表
    【BZOJ 4605】崂山白花蛇草水 替罪羊树套线段树
    假的kd-tree小结
    【BZOJ 1492】 [NOI2007]货币兑换Cash 斜率优化DP
    CDQZ 2017 游记
    【BZOJ 1146】[CTSC2008]网络管理Network
    联赛之前的题表(已完成)汇总(可能有遗漏)
  • 原文地址:https://www.cnblogs.com/jklongint/p/4418994.html
Copyright © 2020-2023  润新知