P4146 序列终结者

splay模板题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e4 + 5;
const int inf = 1e9;
int a[maxn], key[maxn], siz[maxn], fa[maxn], cnt[maxn];
int tag[maxn], add[maxn], mx[maxn], ch[maxn][2];
int root, tot;
inline int read()
{
    int s = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9'){ (s *= 10) += ch-'0'; ch = getchar(); }
    return s*f;
}
void pushup(int x)
{
    siz[x] = siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
    mx[x] = max(key[x],max(mx[ch[x][0]],mx[ch[x][1]]));
}
void pushdown(int x)
{
    int ls = ch[x][0], rs = ch[x][1];
    if(add[x]){
        if(ls) add[ls] += add[x], key[ls] += add[x], mx[ls] += add[x];
        if(rs) add[rs] += add[x], key[rs] += add[x], mx[rs] += add[x];
    }
    if(tag[x]) tag[ls] ^= 1, tag[rs] ^= 1, swap(ch[x][0],ch[x][1]);
    tag[x] = add[x] = 0;
}
int build(int f,int l,int r)
{
    if(l > r) return 0;
    int now = ++tot, mid = (l+r)>>1;
    key[now] = mx[now] = a[mid]; fa[now] = f;
    cnt[now]++;
    ch[now][0] = build(now,l,mid-1);
    ch[now][1] = build(now,mid+1,r);
    pushup(now);
    return now;
}
int get(int x){ return ch[fa[x]][1] == x; }
void Rotate(int x)
{
    int old = fa[x], oldf = fa[old], k = get(x);
    pushdown(old); pushdown(x);
    ch[old][k] = ch[x][k^1]; fa[ch[old][k]] = old;
    ch[x][k^1] = old; fa[old] = x;
    fa[x] = oldf;
    if(oldf) ch[oldf][ch[oldf][1] == old] = x;
    pushup(old); pushup(x);
}
void splay(int x,int goal)
{
    for(int f; (f = fa[x]) != goal; Rotate(x)){
        if(fa[f] != goal){
            Rotate((get(f) == get(x) ? f : x));
        }
    }
    if(!goal) root = x;
}
int query_Kth(int x)
{
    int now = root;
    while(now){
        pushdown(now);
        if(x <= siz[ch[now][0]]) now = ch[now][0];
        else{
            x -= siz[ch[now][0]]+cnt[now];
            if(x <= 0) return now;
            now = ch[now][1];
        }
    }
    return 0;
}
void change(int l,int r)
{
    l = query_Kth(l); r = query_Kth(r+2);
    splay(l,0); splay(r,l);
}
int main()
{
    int n, m, op, l, r, x;
    n = read(); m = read();
    a[1] = -inf; a[n+2] = inf; mx[0] = -inf;
    root = build(0,1,n+2);
    while(m--){
        op = read(); l = read(); r = read();
        change(l,r);
        int k = ch[ch[root][1]][0];
        if(op == 1){
            x = read(); add[k] += x; mx[k] += x; key[k] += x;
        }
        else if(op == 2) tag[k] ^= 1;
        else printf("%d\n",mx[k]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值