序列终结者

原题地址:序列终结者
(做了这道题就不能再作序列了qaq)
fhq treap大法好啊!
标记下传的进阶之题
和会有负数,初始化要-INF(敲重点!)
建议下传之前先更改值,才可以保证如果有标记仍未传完,当前答案也是对的
贴代码

#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAXN 100010
int n, m, root, cnt;
struct node {
    int l, r, val, max, key, size, tag, add;
} t[MAXN];
inline int read() {
    int s = 0, w = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
    for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
    return s * w;
}
inline int New(int val) {
    t[++cnt].val = val, t[cnt].key = rand() * rand(), t[cnt].size = 1;
    return cnt;
}
inline void update(int now) {
    t[0].add = 0;
    t[0].val = 0;
    t[0].max = -2e9;
    t[now].size = t[t[now].l].size + t[t[now].r].size + 1;
    t[now].max = max(t[now].val, max(t[t[now].l].max, t[t[now].r].max));
}
inline void pushdown(int now) {
    t[0].add = 0;
    t[0].val = 0;
    t[0].max = -2e9;
    if (t[now].tag) {
        swap(t[now].l, t[now].r);
        t[t[now].l].tag ^= 1, t[t[now].r].tag ^= 1;
        t[now].tag = 0;
    }
    if (t[now].add) {
        t[t[now].l].add += t[now].add, t[t[now].r].add += t[now].add;
        t[t[now].l].val += t[now].add, t[t[now].r].val += t[now].add;
        t[t[now].l].max += t[now].add, t[t[now].r].max += t[now].add;
        t[now].add = 0;
    }
}
inline void Split(int now, int w, int &u, int &v) {
    if (!now) u = v = 0;
    else {
        pushdown(now);
        if (t[t[now].l].size < w)
            u = now, Split(t[now].r, w - t[t[now].l].size - 1, t[now].r, v);
        else
            v = now, Split(t[now].l, w, u, t[now].l);
        update(now);
    }
}
inline int Merge(int u, int v) {
    if (!u || !v) return u + v;
    if (t[u].key < t[v].key) {
        pushdown(u);
        t[u].r = Merge(t[u].r, v);
        update(u);
        return u;
    }
    else {
        pushdown(v);
        t[v].l = Merge(u, t[v].l);
        update(v);
        return v;
    }
}
int main() {
    n = read(), m = read();
    for (register int i = 1; i <= n; i++)
        root = Merge(root, New(0));
    while (m--) {
        int opt, l, r, v, x, y, z;
        opt = read();
        if (opt == 1) {
            l = read(), r = read(), v = read();
            Split(root, r, x, y);
            Split(x, l - 1, x, z);
            t[z].add += v;
            t[z].val += v;
            t[z].max += v;
            root = Merge(Merge(x, z), y);
        }
        if (opt == 2) {
            l = read(), r = read();
            Split(root, r, x, y);
            Split(x, l - 1, x, z);
            t[z].tag ^= 1;
            root = Merge(Merge(x, z), y);
        }
        if (opt == 3) {
            l = read(), r = read();
            Split(root, r, x, y);
            Split(x, l - 1, x, z);
            printf("%d\n", t[z].max);
            root = Merge(Merge(x, z), y);
        }
    }
    return 0;
}

你可能感兴趣的:(题解,序列终结者,fhq,treap)