跳到正文

个人记录

P3391 【模板】文艺平衡树

yqzx0307约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年5月4日
收录
2026年9月18日
更新
2026年11月14日
篇幅
约 14 分钟

版本

  1. v1当前

文艺平衡树(Splay)

朴素版
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int maxn=1e5+10;
int n,m,l,r;
typedef struct SplayTree
{
    int rt,idx;
    int fa[maxn],ch[maxn][2],cnt[maxn],lz[maxn],siz[maxn],val[maxn];
    bool cid(int x)
    {
        return x==ch[fa[x]][1];
    }
    void pushup(int x)
    {
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
    }
    void rotate_(int x)
    {
        int y=fa[x],z=fa[y];
        int id=cid(x);
        ch[y][id]=ch[x][!id];
        ch[x][!id]=y;
        if(z)
        {
            ch[z][cid(y)]=x;
        }

        if(ch[y][id])
        {
            fa[ch[y][id]]=y;
        }
        fa[y]=x,fa[x]=z;

        pushup(y),pushup(x);
    }
    void splay(int &z,int x)
    {
        int y=fa[x],f=fa[z];
        while(y!=f)
        {
            if(fa[y]!=f)
            {
                rotate_(cid(x)==cid(y)?y:x);
            }
            rotate_(x);
            y=fa[x];
        }
        z=x;
    }
    void build(int n)
    {
        for(int i=0;i<=n+1;++i)
        {
            ++idx;
            ch[idx][0]=rt;
            if(rt)
            {
                fa[rt]=idx;
            }
            rt=idx;
            val[idx]=i;
            ++cnt[idx];
        }
        splay(rt,1);
    }
    void lzrev(int x)
    {
        swap(ch[x][0],ch[x][1]);
        lz[x]^=1;
    }
    void pushdown(int x)
    {
        if(lz[x])
        {
            if(ch[x][0])
            {
                lzrev(ch[x][0]);
            }
            if(ch[x][1])
            {
                lzrev(ch[x][1]);
            }
            lz[x]=0;
        }
    }
    void find_kth(int &rt,int k)
    {
        if(k<0)
        {
            k+=siz[rt]+1;
        }
        if(k<0||k>siz[rt])
        {
            return ;
        }
        int x=rt;
        while(1)
        {
            pushdown(x);
            if(siz[ch[x][0]]>=k)
            {
                x=ch[x][0];
            }
            else if(siz[ch[x][0]]+cnt[x]>=k)
            {
                break;
            }
            else
            {
                k-=siz[ch[x][0]]+cnt[x];
                x=ch[x][1];
            }
        }
        splay(rt,x);
    }
    void rev(int l,int r)
    {
        find_kth(rt,l);
        find_kth(ch[rt][1],r+2-l);
        int x=ch[ch[rt][1]][0];
        lzrev(x);
        pushdown(x);
        splay(rt,x);
    }
    void prt(int x)
    {
        if(!x)
        {
            return ;
        }
        pushdown(x);
        prt(ch[x][0]);
        printf("%lld ",val[x]);
        prt(ch[x][1]);
    }
    void print()
    {
        find_kth(rt,1);
        find_kth(ch[rt][1],-1);
        prt(ch[ch[rt][1]][0]);
        printf("\n");
    }
}SplayTree;
SplayTree splay;
signed main()
{
    scanf("%lld%lld",&n,&m);
    splay.build(n);
    while(m--)
    {
        scanf("%lld%lld",&l,&r);
        splay.rev(l,r);
    }
    splay.print();
    return 0;
}
豆包注释版
C++
// 引入C++所有常用库
#include<bits/stdc++.h>

// 把int换成long long,防止数字太大溢出
#define int long long

// 使用标准命名空间
using namespace std;

// 数组最大长度:1e5+10 = 100010
constexpr int maxn=1e5+10;

// 全局变量:n=数字个数,m=操作次数,l=翻转左端点,r=翻转右端点
int n,m,l,r;

// 定义伸展树结构体(用来实现区间翻转)
typedef struct SplayTree
{
    int rt,idx;        // rt=树根,idx=节点编号
    int fa[maxn];      // fa[x]:x的父节点
    int ch[maxn][2];   // ch[x][0]左孩子,ch[x][1]右孩子
    int cnt[maxn];     // cnt[x]:x节点出现次数
    int lz[maxn];      // lz[x]:翻转懒标记
    int siz[maxn];     // siz[x]:x子树大小
    int val[maxn];     // val[x]:x节点存的值

    // 判断x是父节点的左孩子(0)还是右孩子(1)
    bool cid(int x)
    {
        return x==ch[fa[x]][1];
    }

    // 更新子树大小
    void pushup(int x)
    {
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
    }

    // 旋转操作:调整树结构
    void rotate_(int x)
    {
        int y=fa[x],z=fa[y];    // y是x的爸爸,z是x的爷爷
        int id=cid(x);          // 判断x是左/右孩子

        ch[y][id]=ch[x][!id];   // 把x的反方向孩子给y
        ch[x][!id]=y;           // 把y变成x的反方向孩子

        if(z) ch[z][cid(y)]=x;  // 爷爷的孩子改成x
        if(ch[y][id]) fa[ch[y][id]]=y;  // 更新孩子的父节点

        fa[y]=x; fa[x]=z;       // 更新父子关系
        pushup(y); pushup(x);   // 更新子树大小
    }

    // 伸展操作:把x转到z位置
    void splay(int &z,int x)
    {
        int y=fa[x],f=fa[z];
        while(y!=f)  // 循环旋转直到到位
        {
            if(fa[y]!=f) rotate_(cid(x)==cid(y)?y:x);
            rotate_(x);
            y=fa[x];
        }
        z=x;
    }

    // 建树:建立n个节点的伸展树
    void build(int n)
    {
        for(int i=0;i<=n+1;++i)
        {
            idx++;                 // 节点编号+1
            ch[idx][0]=rt;         // 新节点左孩子=当前根
            if(rt) fa[rt]=idx;     // 旧根的父亲=新节点
            rt=idx;                // 更新根
            val[idx]=i;            // 节点值=i
            cnt[idx]++;            // 计数+1
        }
        splay(rt,1);
    }

    // 给x打翻转标记:交换左右孩子
    void lzrev(int x)
    {
        swap(ch[x][0],ch[x][1]);
        lz[x]^=1;
    }

    // 下放翻转标记:把标记传给孩子
    void pushdown(int x)
    {
        if(lz[x])  // 如果有翻转标记
        {
            if(ch[x][0]) lzrev(ch[x][0]);  // 左孩子翻转
            if(ch[x][1]) lzrev(ch[x][1]);  // 右孩子翻转
            lz[x]=0;  // 清空自己的标记
        }
    }

    // 找到第k个节点,并转到根
    void find_kth(int &rt,int k)
    {
        if(k<0) k+=siz[rt]+1;        // 负数=倒数第k个
        if(k<0||k>siz[rt]) return ;  // 越界直接返回

        int x=rt;
        while(1)
        {
            pushdown(x);  // 先下放标记
            if(siz[ch[x][0]]>=k) x=ch[x][0];            // 去左子树
            else if(siz[ch[x][0]]+cnt[x]>=k) break;      // 找到目标
            else k-=siz[ch[x][0]]+cnt[x],x=ch[x][1];     // 去右子树
        }
        splay(rt,x);
    }

    // 翻转区间 [l, r]
    void rev(int l,int r)
    {
        find_kth(rt,l);                        // 找到第l个
        find_kth(ch[rt][1],r+2-l);             // 找到r+1个
        int x=ch[ch[rt][1]][0];                // 取出要翻转的区间
        lzrev(x);                              // 翻转
        pushdown(x);                           // 下放标记
        splay(rt,x);                           // 伸展调整
    }

    // 中序遍历打印(按顺序输出)
    void prt(int x)
    {
        if(!x) return ;       // 空节点直接返回
        pushdown(x);          // 下放标记
        prt(ch[x][0]);        // 打印左子树
        printf("%lld ",val[x]);// 打印自己
        prt(ch[x][1]);        // 打印右子树
    }

    // 打印整个序列
    void print()
    {
        find_kth(rt,1);               // 找到第一个数
        find_kth(ch[rt][1],-1);       // 找到最后一个数
        prt(ch[ch[rt][1]][0]);        // 打印中间所有数
        printf("\n");
    }
}SplayTree;

// 定义一棵伸展树
SplayTree splay;

// 主函数:程序入口
signed main()
{
    scanf("%lld%lld",&n,&m);   // 输入n和m
    splay.build(n);           // 建树
    while(m--)                // 执行m次翻转
    {
        scanf("%lld%lld",&l,&r);  // 输入翻转区间l~r
        splay.rev(l,r);           // 翻转
    }
    splay.print();            // 输出最终结果
    return 0;
}

P3391 【模板】文艺平衡树

原文