Splay 模板题 一 hdu 3487 play with chain

注意push_down时两个儿子的rev都是^1,而不是变成一.
注意行末不可以有空格.

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int M=300005;
int ch[M][2],siz[M],rev[M],fa[M],val[M];
int n,m,sz,cnt[M];
vector<int>ans;
struct Splay_Tree{
    #define key_tree ch[ch[root][1]][0]
    int root;
    void Newnode(int&rt,int x){
        rt=++sz;
        rev[rt]=ch[rt][0]=ch[rt][1]=0;
        val[rt]=x;cnt[rt]=1;
    }
    void push_up(int rt){
        int l=ch[rt][0],r=ch[rt][1];
        siz[rt]=siz[l]+siz[r]+cnt[rt];
    }
    void push_down(int rt){
        if(rev[rt]){
            int&l=ch[rt][0],&r=ch[rt][1];
            swap(l,r);
            rev[l]^=1;
            rev[r]^=1;
            rev[rt]=0;
        }
    }
    void build(int L,int R,int&rt,int f){
        if(L>R) return;
        int mid=(L+R)>>1;
        Newnode(rt,mid);
        fa[rt]=f;
        build(L,mid-1,ch[rt][0],rt);
        build(mid+1,R,ch[rt][1],rt);
        push_up(rt);
    }
    int find(int k){
        int x=root;
        while(k){
            push_down(x);
            if(siz[ch[x][0]]>=k) x=ch[x][0];
            else if(siz[ch[x][0]]+cnt[x]<k){
                k-=siz[ch[x][0]]+cnt[x];
                x=ch[x][1];
            }else return x;
        }
        return x;
    }
    void Splay(int x,int goal){=
        push_down(x);
        while(fa[x]!=goal){
            int y=fa[x];
            if(fa[y]==goal){
                push_down(y);push_down(x);
                rotate(x,ch[y][0]==x);
            }else{
                int z=fa[y];
                push_down(z);push_down(y);push_down(x);
                bool kind=ch[z][0]==y;
                if(ch[y][kind]==x) rotate(x,!kind);
                else rotate(y,kind);
                rotate(x,kind);
            }
        }
        push_up(x);
        if(goal==0) root=x;
    }
    void rotate(int x,bool f){
        int y=fa[x];
        fa[ch[x][f]]=y;
        ch[y][!f]=ch[x][f];
        if(fa[y]) ch[fa[y]][ch[fa[y]][1]==y]=x;
        fa[x]=fa[y];
        ch[x][f]=y;
        fa[y]=x;
        push_up(y);
    }
    void cut_to(int a,int b,int c){
        int p1=find(a-1),p2=find(b+1);
        // printf("%d %d\n",val[p1],val[p2]);
        Splay(p1,0);
        Splay(p2,root);
        int kt=key_tree;
        key_tree=0;
        // fa[kt]=0;
        push_up(ch[root][1]);
        push_up(root);
        int p3=find(c),p4=find(c+1);
        Splay(p3,0);
        Splay(p4,root);
        key_tree=kt;
        fa[kt]=ch[root][1];
        push_up(ch[root][1]);
        push_up(root);
        // Travel(root);
    }
    void flip(int a,int b){
        // printf("%d %d\n",a-1,b+1);
        int p1=find(a-1),p2=find(b+1);
        Splay(p1,0);
        Splay(p2,p1);
        rev[key_tree]^=1;
        Splay(root,0);
    }
    void Travel(int x){
        if(x){
            printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d \n",x,ch[x][0],ch[x][1],fa[x],siz[x],val[x]);
            Travel(ch[x][0]);
            Travel(ch[x][1]);
        }
    }
    void travel(int x){
        if(!x) return;
        push_down(x);
        travel(ch[x][0]);
        if(val[x]!=1&&val[x]!=n+2) ans.push_back(val[x]-1);
        travel(ch[x][1]);
    }
}spt;
char str[20];
void solve(){
    sz=0;
    ans.clear();
    spt.build(1,n+2,spt.root,0);
    for(int i=0;i<m;i++){
        int a,b,c;
        scanf("%s%d%d",str,&a,&b);
        a++;b++;
        switch(str[0]){
            case 'C':
                scanf("%d",&c);
                c++;
                spt.cut_to(a,b,c);
                break;
            case 'F':
                spt.flip(a,b);
                break;
        }
    }
    spt.travel(spt.root);
    for(int i=0;i<ans.size();i++)
        printf("%d%c",ans[i]," \n"[i+1==ans.size()]);
}
int main(){
    while(scanf("%d%d",&n,&m),(~n||~m)) solve();
    return 0;
}
  • 0
    点赞
  • 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、付费专栏及课程。

余额充值