splay最终模板

时间:2023-02-08 21:21:20

来自wjmzbmr的splay模板

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
 + ;
;
struct Node {
    Node*ch[], *p;
    int size, val, mx;
    int add;
    bool rev;
    Node() {
        size = ;
        val = mx = -INF;
        add = ;
    }
    bool d() {
        ];
    }
    void setc(Node*c, int d) {
        ch[d] = c;
        c->p = this;
    }
    void addIt(int ad) {
        add += ad;
        mx += ad;
        val += ad;
    }
    void revIt() {
        rev ^= ;
    }
    void relax();
    void pu() {
        size = ch[]->size + ch[]->size + ;
        mx = max(val, max(ch[]->mx, ch[]->mx));
    }
} Tnull, *null = &Tnull;
Node mem[MAX_N], *C = mem;
void Node::relax() {
    ) {
        ; i < ; ++i)
            if (ch[i] != null)
                ch[i]->addIt(add);
        add = ;
    }
    if (rev) {
        swap(ch[], ch[]);
        ; i < ; ++i)
            if (ch[i] != null)
                ch[i]->revIt();
        rev = ;
    }
}
Node*make(int v) {
    C->ch[] = C->ch[] = null;
    C->size = ;
    C->val = v;
    C->mx = v;
    C->add = ;
    C->rev = ;
    return C++;
}
Node*build(int l, int r) {
    if (l >= r)
        return null;
    ;
    Node*t = make();
    t->setc(build(l, m), );
    t->setc(build(m + , r), );
    t->pu();
    return t;
}
Node*root;
Node*rot(Node*t) {
    Node*p = t->p;
    p->relax();
    t->relax();
    int d = t->d();
    p->p->setc(t, p->d());
    p->setc(t->ch[!d], d);
    t->setc(p, !d);
    p->pu();
    if (p == root)
        root = t;
}
void splay(Node*t, Node*f = null) {
    while (t->p != f) {
        if (t->p->p == f)
            rot(t);
        else
            t->d() == t->p->d() ? (rot(t->p), rot(t)) : (rot(t), rot(t));
    }
    t->pu();
}
Node* select(int k) {
    for (Node*t = root;;) {
        t->relax();
        ]->size;
        if (k == c)
            return t;
        if (k > c)
            k -= c + , t = t->ch[];
        else
            t = t->ch[];
    }
}
Node*&get(int l, int r) { //[l,r)
    Node*L = );
    Node*R = select(r);
    splay(L);
    splay(R, L);
    ];
}
int n, m;
int main() {
    cin >> n >> m;
    root = build(, n + );
    root->p = null;
    ; i < m; ++i) {
        int k, l, r, v;
        scanf("%d%d%d", &k, &l, &r);
        Node*&t = );
        ) {
            scanf("%d", &v);
            t->addIt(v);
            splay(t);
        } ) {
            t->revIt();
            splay(t);
        } else {
            printf("%d\n", t->mx);
        }
    }
}