Can you answer these queries VI 【SPOJ - GSS6】【Splay 最大子段和】

题目链接


  这道题很容易看到,它是把操作给放到了Splay上面来做的,其实主要改变的就是一些pushup()里面的细节!左右子树没有节点的时候,可千万不要再去比较了!会WA死…… 呜呜呜,当时一直以为自己哪里没考虑到,后来一想,好像就是左右儿子节点不存在,而原来的左右最大是负数的时候,我们假如再去比较,就会得到一个实际上不存在的非负数。

  然后,剩下的,就是基本的操作了,注意细节,Splay真的不大好写…… 呜呜呜…… 写了好久好久。

  然后,为了方便,我在两头分别加入了a[1] = 0, a[N + 2] = 0,并且把原来的1~N变成了2~N+1,这样避免了在查询x-1~x的时候我们要去取x却取不到的情况了(基本操作)

  然后在这里用了加速,直接build了SplayTree,不去一个一个的插入,听说卡常……

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define efs 1e-6
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define max5(a, b, c, d, f) max4(a, b, c, max(d, f))
#define max6(a, b, c, d, f, g) max(max3(a, b, c), max3(d, f, g))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, a[maxN], root = 0, tot;
struct node
{
    int ff, siz, ch[2], lm, rm, mx, all, val;
    node() { ff = siz = ch[0] = ch[1] = lm = rm = mx = all = 0; }
    void get(int x) { lm = rm = mx = all = val = x; }
}t[maxN<<1];
inline void pushup(int rt)
{
    t[rt].lm = t[rt].rm = t[rt].mx = -INF;
    int lc = t[rt].ch[0], rc = t[rt].ch[1];
    t[rt].siz = t[lc].siz + t[rc].siz + 1;
    if(lc) t[rt].lm = t[lc].lm;
    if(rc) t[rt].rm = t[rc].rm;
    t[rt].lm = max3(t[rt].lm, t[lc].all + t[rt].val, t[lc].all + t[rt].val + t[rc].lm);
    t[rt].rm = max3(t[rt].rm, t[rc].all + t[rt].val, t[rc].all + t[rt].val + t[lc].rm);
    if(lc) t[rt].mx = t[lc].mx;
    if(rc) t[rt].mx = max(t[rt].mx, t[rc].mx);
    t[rt].mx = max5(t[rt].mx, t[lc].rm + t[rt].val + t[rc].lm, t[rt].val, t[lc].rm + t[rt].val, t[rc].lm + t[rt].val);
    t[rt].all = t[lc].all + t[rc].all + t[rt].val;
}
void build_Splay(int l, int r, int f)
{
    if(l > r) return;
    int mid = HalF;
    t[f].ch[mid > f] = mid;
    t[mid].ff = f;
    t[mid].siz = 1;
    t[mid].get(a[mid]);
    if(l == r) return;
    build_Splay(l, mid - 1, mid); build_Splay(mid + 1, r, mid);
    pushup(mid);
}
inline void Rotate(int x)
{
    int y = t[x].ff, z = t[y].ff, k = t[y].ch[1] == x;
    t[z].ch[t[z].ch[1] == y] = x;
    t[x].ff = z;
    t[y].ch[k] = t[x].ch[k^1];
    t[t[x].ch[k^1]].ff = y;
    t[x].ch[k^1] = y;
    t[y].ff = x;
    pushup(y); pushup(x);
}
inline void Splay(int x, int goal)
{
    while(t[x].ff != goal)
    {
        int y = t[x].ff, z = t[y].ff;
        if(z != goal) (t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? Rotate(x) : Rotate(y);
        Rotate(x);
    }
    if(!goal) root = x;
}
inline int Kth(int x)  //返回的是位置
{
    int u = root;
    if(t[u].siz < x) return 0;
    while(true)
    {
        int y = t[u].ch[0];
        if(x > t[y].siz + 1)
        {
            x -= t[y].siz + 1;
            u = t[u].ch[1];
        }
        else
        {
            if(t[y].siz >= x) u = y;
            else return u;
        }
    }
}
inline void Insert(int x, int val)
{
    int las = Kth(x), nex = Kth(x + 1);
    Splay(las, 0); Splay(nex, las);
    int u = nex;
    t[u].ch[0] = ++tot;
    u = tot;
    t[u].ff = nex;
    t[u].siz = 1;
    t[u].get(val);
    pushup(nex);
    pushup(las);
}
inline void Delet(int x)
{
    int las = Kth(x - 1), nex = Kth(x + 1);
    Splay(las, 0); Splay(nex, las);
    t[nex].ch[0] = 0;
    pushup(nex);
    pushup(las);
}
inline void Reset(int x, int val)
{
    int now = Kth(x); Splay(now, 0);
    t[now].get(val);
    pushup(now);
}
inline int query(int x, int y)
{
    int las = Kth(x - 1), nex = Kth(y + 1);
    Splay(las, 0); Splay(nex, las);
    int u = t[nex].ch[0];
    return t[u].mx;
}
int main()
{
    scanf("%d", &N);
    for(int i=2; i<=N + 1; i++) scanf("%d", &a[i]);
    tot = N + 2;
    a[1] = a[tot] = 0;
    build_Splay(1, N + 2, root);
    t[0].siz = 0; t[0].get(0); t[0].ch[0] = t[0].ch[1] = 0; t[0].ff = 0;
    root = (N + 3) >> 1;
    scanf("%d", &M);
    char op[3];
    int x, y;
    while(M--)
    {
        scanf("%s", op);
        if(op[0] == 'I')    //在x和x-1之间插入y——这里变成x~x+1之间插入一个数
        {
            scanf("%d%d", &x, &y);
            Insert(x, y);
        }
        else if(op[0] == 'D')   //删除x位置
        {
            scanf("%d", &x); x++;
            Delet(x);
        }
        else if(op[0] == 'R')   //用y替换x位置上的元素
        {
            scanf("%d%d", &x, &y); x++;
            Reset(x, y);
        }
        else    //查询x~y的最大子段和
        {
            scanf("%d%d", &x, &y); x++; y++;
            printf("%d\n", query(x, y));
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值