洛谷3391 【模板】文艺平衡树 Splay

题目

这是一道经典的Splay模板题——文艺平衡树。

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

题解

模板题!
但是我不会区间操作
其实不难,其它操作不变,给每个点打上标记,翻转区间就是这棵子树的每个节点左右儿子翻转。翻转区间l—r时把l-1弄到根节点,r+1弄到根节点下,这样根节点的右儿子的左儿子及它的子树就是该区间,就把这个点异或1。输出时或旋转时下放标记,就是左右子树交换,左右儿子标记异或1,该节点标记为0。

代码

#include <cstdio>

using namespace std;

struct node{
    int l,r,bz,data,fa,size;
}t[100005];
int n,m,root,cnt;

int build(int l,int r,int f){
    if (l>r) return 0;
    int c=++cnt;
    t[c].fa=f;
    t[c].size=r-l+1;
    t[c].data=(l+r)/2;
    t[c].l=build(l,(l+r)/2-1,c);
    t[c].r=build((l+r)/2+1,r,c);
    return c;
}

void pushdown(int x){
    if (t[x].bz){
        t[t[x].l].bz^=1;
        t[t[x].r].bz^=1;
        t[x].bz=0;
        int l=t[x].l,r=t[x].r;
        t[x].l=r;t[x].r=l;
    }   
}

void rttn(int x){
    int y=t[x].l;
    pushdown(x);
    if (t[t[x].fa].l==x) t[t[x].fa].l=y;
                    else t[t[x].fa].r=y;
    t[y].fa=t[x].fa;
    t[x].fa=y;
    t[x].l=t[y].r;
    t[t[y].r].fa=x;
    t[y].r=x;
    if (x==root) root=y;
    t[x].size=t[t[x].l].size+t[t[x].r].size+1;
    t[y].size=t[t[y].l].size+t[t[y].r].size+1;
}

void lttn(int x){
    int y=t[x].r;
    pushdown(x);
    if (t[t[x].fa].l==x) t[t[x].fa].l=y;
                    else t[t[x].fa].r=y;
    t[y].fa=t[x].fa;
    t[x].fa=y;
    t[x].r=t[y].l;
    t[t[y].l].fa=x;
    t[y].l=x;
    if (x==root) root=y;
    t[x].size=t[t[x].l].size+t[t[x].r].size+1;
    t[y].size=t[t[y].l].size+t[t[y].r].size+1;
}

void splay(int x,int r){
    while (t[x].fa!=r){
        int f=t[x].fa,p=t[f].fa;
        if (p==r)
            if (t[f].l==x) rttn(f);
                      else lttn(f);
        else {
            if (t[f].l==x)
                if (t[p].l==f) rttn(f),rttn(p);
                          else rttn(f),lttn(p);
            else
                if (t[p].l==f) lttn(f),rttn(p);
                          else lttn(f),lttn(p);
        }
    }
}

int Find(int k){
    int x=root;
    while (k){
        pushdown(x);
        if (t[t[x].l].size+1==k) return x;
        if (t[t[x].l].size+1<k) k-=t[t[x].l].size+1,x=t[x].r;
                           else x=t[x].l;
    }
    return x;
}

int main(){
    scanf("%d%d",&n,&m);
    root=1;
    build(1,n+2,0);
    for (int i=1;i<=m;i++){
        int l,r,pr,ne;
        scanf("%d%d",&l,&r);
        l++,r++;
        pr=Find(l-1);
        splay(pr,0);
        ne=Find(r+1);
        splay(ne,pr);
        t[t[ne].l].bz^=1;
    }
    for (int i=1;i<=n;i++)
        printf("%d ",t[Find(i+1)].data-1);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我可以为你生成一段splay平衡树代码: // 插入代码 void SplayTree::insert(int key) { //如果根为空,则新节点作为根节点 if (root == NULL) { root = new SplayNode(key); return; } // 寻找节点 SplayNode *current = root, *parent = NULL; while (current != NULL) { parent = current; if (key < current->key) current = current->left; else if (key > current->key) current = current->right; else break; } // 如果key不存在,则插入 if (key != parent->key) { if (key < parent->key) parent->left = new SplayNode(key); else parent->right = new SplayNode(key); // 将新插入节点作为根节点 splay(parent); } } // 删除代码 void SplayTree::remove(int key) { SplayNode *node = search(key); if (node == NULL) return; // 如果没有孩子节点,则直接删除 if (node->left == NULL && node->right == NULL) { if (node->parent == NULL) { root = NULL; } else { if (node->parent->left == node) node->parent->left = NULL; else node->parent->right = NULL; } } // 如果有一个孩子节点,则将其孩子节点替换 else if (node->left == NULL || node->right == NULL) { SplayNode *child = (node->left)? node->left: node->right; if (node->parent == NULL) root = child; else { if (node->parent->left == node) node->parent->left = child; else node->parent->right = child; } } // 如果有两个孩子节点,则找到其后继节点,并将后继节点替换 else { SplayNode *successor = minimum(node->right); if (node->parent == NULL) root = successor; else { if (node->parent->left == node) node->parent->left = successor; else node->parent->right = successor; } successor->left = node->left; } delete(node); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值