• BZOJ1513 [POI2006]Tet-Tetris 3D


    裸的二维线段树。。。求矩形最大值和矩形覆盖

    板子敲起来比较麻烦QAQ

      1 /**************************************************************
      2     Problem: 1513
      3     User: rausen
      4     Language: C++
      5     Result: Accepted
      6     Time:24884 ms
      7     Memory:127268 kb
      8 ****************************************************************/
      9  
     10 #include <cstdio>
     11 #include <algorithm>
     12  
     13 using namespace std;
     14  
     15 inline int read();
     16  
     17 int n, m;
     18  
     19 struct segx {
     20     segx *ls, *rs;
     21     int mx, tag;
     22      
     23     segx() {
     24         ls = rs = NULL, mx = tag = 0;
     25     }
     26      
     27     #define Len (1 << 16)
     28     inline void* operator new(size_t) {
     29         static segx *mempool, *c;
     30         if (c == mempool)
     31             mempool = (c = new segx[Len]) + Len;
     32         *c = segx();
     33         return c++;
     34     }
     35     #undef Len
     36      
     37     #define mid (l + r >> 1)
     38     void build(int l, int r) {
     39         if (l == r) return;
     40         ls = new()segx, rs = new()segx;
     41         ls -> build(l, mid), rs -> build(mid + 1, r);
     42     }
     43      
     44     void modify(int l, int r, int L, int R, int v) {
     45         mx = max(mx, v);
     46         if (L <= l && r <= R) {
     47             tag = max(tag, v);
     48             return;
     49         }
     50         if (L <= mid) ls -> modify(l, mid, L, R, v);
     51         if (mid < R) rs -> modify(mid + 1, r, L, R, v);
     52     }
     53      
     54     int query(int l, int r, int L, int R) {
     55         if (L <= l && r <= R) return mx;
     56         int res = tag;
     57         if (L <= mid) res = max(res, ls -> query(l, mid, L, R));
     58         if (mid < R) res = max(res, rs -> query(mid + 1, r, L, R));
     59         return res;
     60     }
     61     #undef mid
     62 };
     63  
     64 struct segy {
     65     segy *ls, *rs;
     66     segx *mx, *tag;
     67      
     68     segy() {
     69         ls = rs = NULL, mx = tag = NULL;
     70     }
     71      
     72     #define Len (1 << 16)
     73     inline void* operator new(size_t) {
     74         static segy *mempool, *c;
     75         if (c == mempool)
     76             mempool = (c = new segy[Len]) + Len;
     77         *c = segy();
     78         return c++;
     79     }
     80     #undef Len
     81      
     82     #define mid (l + r >> 1)
     83     void build(int l, int r) {
     84         mx = new()segx, tag = new()segx;
     85         mx -> build(1, m), tag -> build(1, m);
     86         if (l == r) return;
     87         ls = new()segy, rs = new()segy;
     88         ls -> build(l, mid), rs -> build(mid + 1, r);
     89     }
     90      
     91     void modify(int l, int r, int L, int R, int x, int y, int v) {
     92         mx -> modify(1, m, x, y, v);
     93         if (L <= l && r <= R) {
     94             tag -> modify(1, m, x, y, v);
     95             return;
     96         }
     97         if (L <= mid) ls -> modify(l, mid, L, R, x, y, v);
     98         if (mid < R) rs -> modify(mid + 1, r, L, R, x, y, v);
     99     }
    100      
    101     int query(int l, int r, int L, int R, int x, int y) {
    102         if (L <= l && r <= R) return mx -> query(1, m, x, y);
    103         int res = tag -> query(1, m, x, y);
    104         if (L <= mid) res = max(res, ls -> query(l, mid, L, R, x, y));
    105         if (mid < R) res = max(res, rs -> query(mid + 1, r, L, R, x, y));
    106         return res;
    107     }
    108     #undef mid
    109 } *T;
    110  
    111 int main() {
    112     int x, y, dx, dy, h, Tot;
    113     n = read(), m = read(), Tot = read();
    114     T = new()segy;
    115     T -> build(1, n);
    116     while (Tot--) {
    117         dx = read(), dy = read(), h = read(), x = read(), y = read();
    118         T -> modify(1, n, x + 1, x + dx, y + 1, y + dy, T -> query(1, n, x + 1, x + dx, y + 1, y + dy) + h);
    119     }
    120     printf("%d
    ", T -> query(1, n, 1, n, 1, m));
    121     return 0;
    122 }
    123  
    124 inline int read() {
    125     static int x;
    126     static char ch;
    127     x = 0, ch = getchar();
    128     while (ch < '0' || '9' < ch)
    129         ch = getchar();
    130     while ('0' <= ch && ch <= '9') {
    131         x = x * 10 + ch - '0';
    132         ch = getchar();
    133     }
    134     return x;
    135 }
    View Code
    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    我总结的面试题系列:kafka
    RabbitMQ大厂面试题
    [Algorithm] 并查集
    [LintCode] 编辑距离
    [LeetCode] Length of Longest Fibonacci Subsequence
    [LintCode] 交叉字符串
    [LeetCode] Permutation Sequence
    Permutation Sequence
    [LeetCode] Next Permutation
    [LeetCode] Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/rausen/p/4474817.html
Copyright © 2020-2023  润新知