跳到正文

剪贴板

剪贴板 17g38e9q

原文
剪贴板信息

剪贴板信息

收录
更新

FHQ-treap P2042维护序列

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define FL(a, b, c) for(int a = (b); a <= (c); a++)
#define FR(a, b, c) for(int a = (b); a >= (c); a--)
mt19937 myrd(chrono::steady_clock::now().time_since_epoch().count());
#define rd(l, r) uniform_int_distribution<int>(l, r)(myrd)
#define INF -10000
constexpr int N = 5e5 + 10;
struct treap{
    struct node{int lmax, max, sum, ls, rs, rmax, rev, val, rank, cov, size;}d[N];
    int tot = 0, top = 0, stk[N];
    int newnode(int val){
        int x = top ? stk[top--] : ++tot;node &z = d[x];z.size = 1;
        z.max = z.sum = z.val = val, z.cov = INF, z.lmax = z.rmax = max(0, val);
        z.rank = rd(1, 10000000), z.ls = z.rs = z.rev = 0;return x;
    }
    void del(int u){
        stk[++top] = u;
        (d[u].ls) && (del(d[u].ls), 1);
        (d[u].rs) && (del(d[u].rs), 1);
    }
    void pushup(int u){
        node &z = d[u], &ls = d[z.ls], &rs = d[z.rs];
        z.sum = z.val + ls.sum + rs.sum, z.size = 1 + ls.size + rs.size;
        z.lmax = max(ls.lmax, ls.sum + z.val + rs.lmax);
        z.rmax = max(rs.rmax, rs.sum + z.val + ls.rmax);
        z.max = max(ls.rmax + rs.lmax, 0) + z.val;
        if(z.rs)z.max = max(z.max, rs.max);
        if(z.ls)z.max = max(z.max, ls.max);
    }
    void change(node &u, int c){
        u.val = u.cov = c, u.sum = u.size * c, u.rev = 0;
        u.max = max(c, u.sum), u.rmax = u.lmax = max(0, u.sum);
    }
    void pushdown(int u){
        node &z = d[u], &ls = d[z.ls], &rs = d[z.rs];
        int &cov = z.cov, &rev = z.rev;
        if(cov != INF){
            if(z.ls)change(ls, cov);
            if(z.rs)change(rs, cov);
            rev = 0, cov = INF;
        }
        if(rev){
            if(z.ls) swap(ls.lmax, ls.rmax), ls.rev ^= 1;
            if(z.rs) swap(rs.rmax, rs.lmax), rs.rev ^= 1;
            swap(z.ls, z.rs), rev = 0;
        }
    }
    void split(int u, int kth, int& L, int& R){
        if(!u)return L = R = 0, void();
        pushdown(u);node &z = d[u];
        if(d[z.ls].size >= kth)R = u, split(z.ls, kth, L, z.ls);
        else L = u, split(z.rs, kth - d[z.ls].size - 1, z.rs, R);
        pushup(u);
    }
    int merge(int L, int R){
        if(!L || !R)return L + R;
        if(d[L].rank < d[R].rank)
        return pushdown(L), d[L].rs = merge(d[L].rs, R), pushup(L), L;
        else return pushdown(R), d[R].ls = merge(L, d[R].ls), pushup(R), R;
    }
}T;
int root;
void insert(){
    int pos, tot;
    cin >> pos >> tot;
    int x = 0, w = 0;
    FL(i, 1, tot)cin >> w, x = T.merge(x, T.newnode(w));
    int L, R;
    T.split(root, pos, L, R);
    root = T.merge(L, T.merge(x, R));
}
void del(){
    int pos, tot;
    cin >> pos >> tot;
    int L, mid, R;
    T.split(root, pos - 1, L, R), T.split(R, tot, mid, R), T.del(mid);
    //cout << T.d[L].size << " " << mid << " " << R << endl;
    root = T.merge(L, R);
}
void cover(){
    int pos, tot, c;
    cin >> pos >> tot >> c;
    int L, mid, R;
    T.split(root, pos - 1, L, R), T.split(R, tot, mid, R);
    T.change(T.d[mid], c);
    root = T.merge(L, T.merge(mid, R));
}
void reverse(){
    int pos, tot, c;
    cin >> pos >> tot;
    int L, mid, R;
    T.split(root, pos - 1, L, R), T.split(R, tot, mid, R);
    T.d[mid].rev ^= 1, swap(T.d[mid].lmax, T.d[mid].rmax);
    root = T.merge(L, T.merge(mid, R));
}
void getsum(){
    int pos, tot, c;
    cin >> pos >> tot;
    int L, mid, R;
    T.split(root, pos - 1, L, R), T.split(R, tot, mid, R);
    cout << T.d[mid].sum << endl;
    root = T.merge(L, T.merge(mid, R));
}
void maxsum(){
    cout << T.d[root].max << endl;
}
int32_t main(){
    //freopen("1.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int n, m, x;
    cin >> n >> m;
    while(n--)cin >> x, root = T.merge(root, T.newnode(x));
    while(m--){
        char s[100];
        cin >> s + 1;
        switch(s[3]){
            case 'S':insert();break;
            case 'L':del();break;
            case 'K':cover();break;
            case 'V':reverse();break;
            case 'T':getsum();break;
            case 'X':maxsum();
        }
    }
    return 0;
}