bzoj 1500 维修数列

splay插入,删除,区间翻转,区间修改,区间求和,区间最大子段和。
要把splay 的删除后的结点编号,重新利用,不然会MLE。初始化的时候,a[0] = a[n+1] = -inf;
sum[0] = 0; lsum[0] = rsum[0] = mx[0] = -inf;不然区间求子段和的时候有负数的时候就跪了(因为lsum[0], rsum[0], mx[0]默认是0)。

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

#pragma comment(linxer, "/STACK:102400000,102400000")
#define LL long long 
#define pii pair<int, int>
#define MP maxe_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 700020
#define M 500010

int ch[N][2], pre[N], sz[N], a[N], val[N];
int sum[N], lsum[N], rsum[N], mx[N], down[N];
int n, root, tot; 
bool f[N], ok;
queue<int> q;

void init(int x){
    if(!x) return ;
    q.push(x);
    init(ch[x][0]);
    init(ch[x][1]);
    ch[x][0] = ch[x][1] = 0;
    sum[x] = lsum[x] = rsum[x] = -inf;
}
int creat(int v, int fa){
    int x = q.front(); q.pop();
    pre[x] = fa;
    ch[x][0] = ch[x][1] = 0;
    val[x] = sum[x] = v;
    mx[x] = lsum[x] = rsum[x] = v;
    sz[x] = 1;
    down[x] = inf;
    f[x] = 0;
    return x;
}
void push_up(int x){
    int lx = ch[x][0], rx = ch[x][1];
    sz[x] = sz[lx] + sz[rx] + 1;
    sum[x] = sum[lx] + sum[rx] + val[x];
    lsum[x] = max(lsum[lx], sum[lx] + max(0, lsum[rx]) + val[x]);
    rsum[x] = max(rsum[rx], sum[rx] + max(0, rsum[lx]) + val[x]);
    mx[x] = max(0, rsum[lx]) + max(0, lsum[rx]) + val[x];
    mx[x] = max(mx[x], max(mx[lx], mx[rx]));
}
void push_lazy(int x, int v){
    sum[x] = sz[x] * v;
    val[x] = v;
    mx[x] = lsum[x] = rsum[x] = max(v, sum[x]);
    down[x] = v;
}
void push_down(int x){
    if(f[x]){
        swap(ch[x][0], ch[x][1]);
        if(ch[x][0]) f[ch[x][0]] ^= 1, swap(lsum[ch[x][0]], rsum[ch[x][0]]);
        if(ch[x][1]) f[ch[x][1]] ^= 1, swap(lsum[ch[x][1]], rsum[ch[x][1]]);
        f[x] = 0;
    }
    if(down[x] != inf){
        if(ch[x][0]) push_lazy(ch[x][0], down[x]);
        if(ch[x][1]) push_lazy(ch[x][1], down[x]);      
        down[x] = inf;
    }
}
int build(int ll, int rr, int fa){
    if(ll > rr) return 0;
    int x = creat(a[md], fa);
    ch[x][0] = build(ll, md - 1, x);
    ch[x][1] = build(md + 1, rr, x);
    push_up(x);
    return x;
}
void rotate(int x){
    int y = pre[x], d = ch[y][1] == x;
    ch[y][d] = ch[x][!d];
    if(ch[x][!d]) pre[ch[x][!d]] = y;
    ch[x][!d] = y;
    pre[x] = pre[y];
    pre[y] = x;
    if(pre[x]) ch[pre[x]][ch[pre[x]][1] == y] = x;
    push_up(y);
}
void P(int x){
    if(pre[x])
        P(pre[x]);
    push_down(x);
}
void splay(int x, int goal){
    P(x);
    while(pre[x] != goal){
        int fa = pre[x], ff = pre[fa];
        if(ff == goal)
            rotate(x);
        else if((ch[ff][1] == fa) == (ch[fa][1] == x))
            rotate(fa), rotate(x);
        else 
            rotate(x), rotate(x);
    }
    push_up(x);
    if(goal == 0) root = x;
}
int Kth(int k){
    int x = root;
    while(x){
        push_down(x);
        if(sz[ch[x][0]] >= k) x = ch[x][0];
        else{
            k -= sz[ch[x][0]] + 1;
            if(k == 0) return x;
            x = ch[x][1];
        }
    }
    return x;
}

void output(int x){
    if(!x) return ;
    push_down(x);
    output(ch[x][0]);
    if(val[x] < inf){
        if(!ok) ok = 1;
        else printf(" ");
        printf("%d", val[x]);
    }
    output(ch[x][1]);
}
void Insert(int k, int cnt){
    splay(Kth(k+1), 0);
    splay(Kth(k+2), root);
    int fa = ch[root][1];
    ch[fa][0] = build(1, cnt, fa);
    push_up(fa);
    push_up(root);
}
void Delete(int L, int R){
    splay(Kth(L), 0);
    splay(Kth(R+2), root);
    int fa = ch[root][1];
    init(ch[fa][0]);
    ch[fa][0] = 0;
    push_up(fa);
    push_up(root);
}
void Makesame(int L, int R, int c){
    splay(Kth(L), 0);
    splay(Kth(R+2), root);
    int fa = ch[root][1];
    push_lazy(ch[fa][0], c);
    push_up(fa);
    push_up(root);
}
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= 700000; ++i)
        q.push(i);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    a[0] = a[n+1] = -inf;
    sum[0] = 0; lsum[0] = rsum[0] = mx[0] = -inf;
    root = build(0, n + 1, 0);
    char s[20];
    int cur, cnt, c;
    while(m--){
        scanf("%s", s);
        if(s[0] == 'I'){
            scanf("%d%d", &cur, &cnt);
            for(int i = 1; i <= cnt; ++i)
                scanf("%d", &a[i]);
            Insert(cur, cnt);
            n += cnt;
        //  output(root); puts("");
        }
        else if(s[0] == 'D'){
            scanf("%d%d", &cur, &cnt);
            Delete(cur, cur + cnt - 1);
            n -= cnt;
        //  output(root); puts("");
        }
        else if(s[0] == 'M' && s[2] == 'K'){
            scanf("%d%d%d", &cur, &cnt, &c);
            Makesame(cur, cur + cnt - 1, c);
        }
        else if(s[0] == 'R'){
            scanf("%d%d", &cur, &cnt);
            int L = cur, R = cur + cnt - 1;
            splay(Kth(L), 0);
            splay(Kth(R+2), root);
            int fa = ch[root][1];
            int x = ch[fa][0];
            f[x] ^= 1;
            swap(lsum[x], rsum[x]);
        }
        else if(s[0] == 'G'){
            scanf("%d%d", &cur, &cnt);
            int L = cur, R = cur + cnt - 1;
            splay(Kth(L), 0);
            splay(Kth(R+2), root);
            int fa = ch[root][1];
            printf("%d\n", sum[ch[fa][0]]);
        }
        else{
            int L = 1, R = n;
            splay(Kth(L), 0);
            splay(Kth(R+2), root);
            int fa = ch[root][1];
            printf("%d\n", mx[ch[fa][0]]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值