BZOJ 4364: [IOI2014]wall砖墙

线段树直接维护区间最大值最小值,修改时直接对最大最小值进行修改
pushdown时将左右儿子的最大最小值卡进父亲的上下界

#include 

namespace IO {
    void read() {}
    template
    inline void read(T &x, T2 &... oth) {
        x = 0; T f = 1; char ch = getchar();
        while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
        x *= f;
        read(oth...);
    }
}

const int N = 2e6 + 7;
int n, m;

inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }

struct Seg {
    #define lp p << 1
    #define rp p << 1 | 1
    int mx[N << 2], mn[N << 2];
    inline void pushup(int p) {
        mx[p] = max(mx[lp], mx[rp]);
        mn[p] = min(mn[lp], mn[rp]);
    }
    inline void tag(int p, int f) {
        if (mn[f] > mx[p]) mn[p] = mx[p] = mn[f];
        else if (mn[f] > mn[p]) mn[p] = mn[f];
        if (mx[f] < mn[p]) mn[p] = mx[p] = mx[f];
        else if (mx[f] < mx[p]) mx[p] = mx[f];
    }
    inline void pushdown(int p) {
        tag(lp, p);
        tag(rp, p);
    }
    void update(int p, int l, int r, int x, int y, int v, int opt) {
        if (x <= l && y >= r) {
            if (opt == 1) {
                mn[p] = max(mn[p], v);
                mx[p] = max(mx[p], v);
            } else {
                mn[p] = min(mn[p], v);
                mx[p] = min(mx[p], v);
            }
            return;
        }
        pushdown(p);
        int mid = l + r >> 1;
        if (x <= mid) update(lp, l, mid, x, y, v, opt);
        if (y > mid) update(rp, mid + 1, r, x, y, v, opt);
        pushup(p);
    }
    void print(int p, int l, int r) {
        if (l == r) {
            //assert(mx[p] == mn[p]);
            printf("%d\n", mx[p]);
            return;
        }
        pushdown(p);
        int mid = l + r >> 1;
        print(lp, l, mid);
        print(rp, mid + 1, r);
    }
} seg;

int main() {
    IO::read(n, m);
    for (int opt, l, r, v; m--; ) {
        IO::read(opt, l, r, v);
        l++, r++;
        seg.update(1, 1, n, l, r, v, opt);
    }
    seg.print(1, 1, n);
    return 0;
}

你可能感兴趣的:(BZOJ 4364: [IOI2014]wall砖墙)