跳到正文

个人记录

splay 板子

SSerxhs约 15 分钟
原文
文章信息与版本

文章信息

发布
2019年6月13日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 15 分钟
获赞
2

版本

  1. v1当前

扔代码用

C++
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int __SIZE=5e5;//数组大小
const int __N=__SIZE+5;
const ll __inf=9e18;
struct splay
{
    private: ll v[__N],s[__N],fg[__N],lz[__N],zd[__N],zx[__N];
    int c[__N][2],f[__N],st[__N],siz[__N];
    int n,rt,tp,i,x;
    bool fz[__N],yf[__N],inited;
    /*public: inline void out(int x)//调试用
    {
        printf("%lld: c%lld&%lld f%lld s%lld v%lld siz%lld\n",x,c[x][0],c[x][1],f[x],s[x],v[x],siz[x]);
        if (c[x][0]) out(c[x][0]);
        if (c[x][1]) out(c[x][1]);
    }
    inline void outt() {out(rt);}*/
    private: inline void pushup(register int x)
    {
        siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
        s[x]=s[c[x][0]]+s[c[x][1]]+v[x];
        zd[x]=max(max(zd[c[x][0]],zd[c[x][1]]),v[x]);
        zx[x]=min(min(zx[c[x][0]],zx[c[x][1]]),v[x]);
    }
    inline void pushdown(register int x)
    {
        register int y;
        if (fz[x])
        {
            if (y=c[x][0])
            {
                swap(c[y][0],c[y][1]);
                fz[y]^=1;
            }
            if (y=c[x][1])
            {
                swap(c[y][0],c[y][1]);
                fz[y]^=1;
            }
            fz[x]=0;
        }
        if (lz[x])
        {
            if (y=c[x][0])
            {
                s[y]+=siz[y]*lz[x],v[y]+=lz[x],zd[y]+=lz[x],zx[y]+=lz[x];
                if (yf[y]) fg[y]+=lz[x]; else lz[y]+=lz[x];
            }
            if (y=c[x][1])
            {
                s[y]+=siz[y]*lz[x],v[y]+=lz[x],zd[y]+=lz[x],zx[y]+=lz[x];
                if (yf[y]) fg[y]+=lz[x]; else lz[y]+=lz[x];
            }
            lz[x]=0;
        }
        if (yf[x])
        {
            if (y=c[x][0])
            {
                yf[y]=1;s[y]=siz[y]*fg[x];
                zd[y]=zx[y]=v[y]=fg[y]=fg[x];
                lz[y]=0;
            }
            if (y=c[x][1])
            {
                yf[y]=1;s[y]=siz[y]*fg[x];
                zd[y]=zx[y]=v[y]=fg[y]=fg[x];
                lz[y]=0;
            }
            yf[x]=0;
        }
    }
    inline void zigzag(register int x)
    {
        register int y=f[x],z=f[y],typ=(c[y][0]==x);
        if (z) c[z][c[z][1]==y]=x;
        f[y]=x;f[x]=z;
        if (c[x][typ]) f[c[x][typ]]=y;
        c[y][typ^1]=c[x][typ];c[x][typ]=y;
        pushup(y);
    }
    inline void _splay(register int x,register int tar)
    {
        register int y;
        if (!tar) rt=x;
        while ((y=f[x])!=tar)
        {
            if (f[y]!=tar)
            {
                if (c[y][0]==x^c[f[y]][0]==y) zigzag(x); else zigzag(y);
            }
            zigzag(x);
        }
        pushup(x);
    }
    inline void find(register int kth,int tar)
    {
        register int x=rt;pushdown(x);
        while (siz[c[x][0]]+1!=kth)
        {
            if (kth<=siz[c[x][0]]) pushdown(x=c[x][0]); else kth-=siz[c[x][0]]+1,pushdown(x=c[x][1]);
        }
        _splay(x,tar);
    }
    inline void split(int x,int y)
    {
        find(x,0);/*puts("FIND");out(rt);*/find(y+2,rt);
    }
    inline void init()
    {
        zx[0]=__inf;zd[0]=-__inf;tp=__SIZE;inited=rt=1;
        for (i=1;i<=__SIZE;i++) st[i]=__SIZE-i+3;
        c[1][1]=2;f[2]=1;
        siz[1]=2;siz[2]=1;
    }
    inline void clear(register int x)
    {
        v[x]=s[x]=fg[x]=lz[x]=zd[x]=zx[x]=0;
        c[x][0]=c[x][1]=f[x]=siz[x]=0;
        fz[x]=yf[x]=0;
    }
    inline int build(int l,int r,ll *a)
    {
        if (l>r) return 0;
        int x=st[tp--],mid=l+r>>1;
        siz[x]=1;v[x]=a[mid];
        if (c[x][0]=build(l,mid-1,a)) f[c[x][0]]=x;
        if (c[x][1]=build(mid+1,r,a)) f[c[x][1]]=x;
        pushup(x);return x;
    }
    inline bool check(int l,int r)
    {
        if (l>r) return puts("l>r!"),1;
        if (l<0) return puts("illegal l!"),1;
        if (r>n) return puts("illegal r!"),1;
        return 0;
    }
    void del(int x)
    {
        if (c[x][0]) del(c[x][0]);
        if (c[x][1]) del(c[x][1]);
        clear(st[++tp]=x);
    }
    void out(int x)
    {
        pushdown(x);
        if (c[x][0]) out(c[x][0]);
        if (x>2) printf("%lld ",v[x]);
        if (c[x][1]) out(c[x][1]);
    }
    public: inline void insert(int pos,int num,ll *a)//在 [pos-1] 和 [pos] 之间插入一段区间,区间个数为 num,区间为 a[],下标从1开始
    {
        if (!inited) init();
        if (num<0) return puts("num should be positive!"),void();
        if (num>tp) return puts("inserted too much numbers!"),void();
        if ((pos<0)||(pos>n+1)) return puts("wrong pos!"),void();
        split(pos,pos-1);pos=c[rt][1];
        f[c[pos][0]=build(1,num,a)]=pos;
        pushup(pos);pushup(f[pos]);n+=num;
    }
    inline void modify_add(int l,int r,ll num)//将区间 [l,r] 所有数增加 num
    {
        if (check(l,r)) return;
        split(l,r);l=c[c[rt][1]][0];
        if (yf[l]) fg[l]+=num; else lz[l]+=num;
        s[l]+=num*siz[l];zd[l]+=num;zx[l]+=num;v[l]+=num;
        pushup(f[l]);pushup(rt);
    }
    inline void modify_cover(int l,int r,ll num)//将区间 [l,r] 所有数变为 num
    {
        if (check(l,r)) return;
        split(l,r);l=c[c[rt][1]][0];
        yf[l]=1;fg[l]+=num;lz[l]=0;
        s[l]=num*siz[l];zd[l]=zx[l]=v[l]=num;
        pushup(f[l]);pushup(rt);
    }
    inline void modify_turn(int l,int r)//将区间 [l,r] 翻转
    {
        if (check(l,r)) return;
        split(l,r);//out(c[c[rt][1]][0]);puts("");
        fz[l=c[c[rt][1]][0]]^=1;swap(c[l][0],c[l][1]);
    }
    inline void modify_move(int l,int r,int pos)//将区间 [l,r] 移动到 [pos-1] 和 [pos] 之间
    {
    	if (pos>r) pos-=r-l+1;
        split(l,r);l=c[c[rt][1]][0];r=f[l];c[r][0]=f[l]=0;pushup(r);pushup(rt);
        split(pos,pos-1);r=c[rt][1];c[f[l]=r][0]=l;pushup(r);pushup(rt);
    }
    inline void modify_delete(int l,int r)//删除区间 [l,r]
    {
        split(l,r);l=c[c[rt][1]][0];r=f[l];c[r][0]=f[l]=0;
        pushup(r);pushup(rt);del(l);
    }
    inline ll ask_sum(int l,int r)//返回 [l,r] 的和
    {
        if (check(l,r)) return 0;
        split(l,r);
        return s[c[c[rt][1]][0]];
    }
    inline ll ask_max(int l,int r)//返回 [l,r] 的最大值
    {
        if (check(l,r)) return 0;
        split(l,r);
        return zd[c[c[rt][1]][0]];
    }
    inline ll ask_min(int l,int r)//返回 [l,r] 的最小值
    {
        if (check(l,r)) return 0;
        split(l,r);
        return zx[c[c[rt][1]][0]];
    }
    inline void output()//输出整个数列
    {
        out(rt);puts("");
    }
};
inline void read(register int &x)
{
    register int c=getchar(),fh=0;
    while ((c<48)||(c>57)) {if (c=='-') fh=1; c=getchar();}
    x=c^48;c=getchar();
    while ((c>=48)&&(c<=57))
    {
        x=x*10+(c^48);
        c=getchar();
    }
    if (fh) x=-x;
}
inline void read(register ll &x)
{
    register int c=getchar(),fh=0;
    while ((c<48)||(c>57)) {if (c=='-') fh=1; c=getchar();}
    x=c^48;c=getchar();
    while ((c>=48)&&(c<=57))
    {
        x=x*10+(c^48);
        c=getchar();
    }
    if (fh) x=-x;
}
splay a;
ll aa[__N*4+4];
int n,m,i,x,y,z;
int main()
{
    read(m);
    while (m--)
    {
        read(x);if (x==0) { a.output(); continue;} read(y);read(z);
        if (x==1) { for (i=1;i<=z;i++) read(aa[i]);a.insert(y,z,aa);}
        else if (x==2) a.modify_delete(y,z);
        else if (x==3) a.modify_turn(y,z);
        else if (x==4) read(x),a.modify_move(y,z,x);
        else if (x==5) read(x),a.modify_add(y,z,x);
        else if (x==6) read(x),a.modify_cover(y,z,x);
        else if (x==7) printf("%lld\n",a.ask_sum(y,z));
        else if (x==8) printf("%lld\n",a.ask_max(y,z));
        else printf("%lld\n",a.ask_min(y,z));
    }
}
原文