【bzoj3223】文艺平衡树【Splay】【呵呵】

传送门:bzoj3223:文艺平衡树
裸的区间翻转啦……

本蒟蒻一开始就犯了一个致命的错误:按照splay节点里存的数来查找节点。

YanBigGod说:应该查第k大。

Orz。

于是乎我查了第k大。

交上去T了。

原来是查第k大的时候卡死了。

还好我写的非递归- -

不然就爆栈了……

但是我发现我又犯了错误:

我原来写的是:

inline int select(int x,int k){
	while(k!=t[t[x].ch[0]].s+1){
		pushdown(x);
		if(k<t[t[x].ch[0]].s+1)
			x=t[x].ch[0];
		else k-=t[t[x].ch[0]].s+1,x=t[x].ch[1];
	}
	return x;
}



这就造成了一个问题:pushdown有可能会改变左右孩子,所以说上面的t[x].ch[0]不一定是下面的t[x].ch[0]!!!

所以改成了:

inline int select(int x,int k){
	while(1){
		pushdown(x);
		if(k==t[t[x].ch[0]].s+1) break;
		if(k<t[t[x].ch[0]].s+1)
			x=t[x].ch[0];
		else k-=t[t[x].ch[0]].s+1,x=t[x].ch[1];
	}
	return x;
}

然后YanBigGod说Splay的时候不必pushdown,因为select的时候已经处理完了。

Orz!!!

但是这样我发现出现了一个很神奇的现象:几次操作以后树根的size居然变大了!!!

多么奇葩- -

一定是指针指来指去指乱套了……

我看pushdown不顺眼,决定改他一改。

	inline void pushdown(int x){
		if(!t[x].flip) return;
		t[t[x].ch[0]].flip^=1;
		t[t[x].ch[1]].flip^=1;
		swap(t[t[x].ch[0]].p,t[t[x].ch[1]].p);
		t[x].flip=false;
		swap(t[x].ch[0],t[x].ch[1]);
	}
改成

	inline void pushdown(int x){
		if(!t[x].flip) return;
		t[t[x].ch[0]].flip^=1;
		t[t[x].ch[0]].p^=1;
		t[t[x].ch[1]].flip^=1;
		t[t[x].ch[1]].p^=1;
		t[x].flip=false;
		swap(t[x].ch[0],t[x].ch[1]);
	}
t[x].p的意思是表示x节点是他爸的右孩子还是左孩子。

关于这种splay详见mato_No1神犇的博客->这里,真短……
居然正常了………………

然后我又看insert不顺眼(我原来那个insert是按照t[x].v插入,如果不是一次插入完成的话就炸了),就改成了递归建平衡树……

代码在这里- -

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,m,l,r;
inline void read(int &x){
    x=0;
    char ch=getchar();
    bool flag=false;
    if(ch==-1) return;
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') flag=true,ch=getchar();
    do{x=x*10+ch-48;ch=getchar();}while(ch>='0'&&ch<='9');
    if(flag) x=-x;
}
inline void print(int x){
    char a[10]={0};
    while(x) a[++a[0]]=x%10,x/=10;
    for(;a[0];--a[0])
        putchar(a[a[0]]+48);
    putchar(' ');
}
 
struct Splay{
    static const int maxn=100011;
    struct node{
        int ch[2],f,v,s;
        bool p,flip;
    }t[maxn+2];
    int rt,tot;
    inline void upd(int x){
        t[x].s=t[t[x].ch[0]].s+t[t[x].ch[1]].s+1;
    }
    inline void pushdown(int x){
        if(!t[x].flip) return;
        t[t[x].ch[0]].flip^=1;
        t[t[x].ch[0]].p^=1;
        t[t[x].ch[1]].flip^=1;
        t[t[x].ch[1]].p^=1;
        t[x].flip=false;
        swap(t[x].ch[0],t[x].ch[1]);
    }
    inline void sc(int a,int b,bool c){
        t[a].ch[c]=b;
        t[b].f=a;
        t[b].p=c;
    }
    inline void rot(int x){
        int b=t[x].f;
        bool p=t[x].p;
        if(b==rt) t[rt=x].f=0;
        else sc(t[b].f,x,t[b].p);
        sc(b,t[x].ch[!p],p);
        sc(x,b,!p);
        upd(b);
    }
    inline void splay(int x,int r){
        int f;
        while((f=t[x].f)!=r){
            if(t[f].f==r) rot(x);
            else if(t[f].p==t[x].p)
                rot(f),rot(x);
            else rot(x),rot(x);
        }
        upd(x);
    }
    inline int select(int x,int k){
        while(1){
            pushdown(x);
            if(k==t[t[x].ch[0]].s+1) break;
            if(k<t[t[x].ch[0]].s+1)
                x=t[x].ch[0];
            else k-=t[t[x].ch[0]].s+1,x=t[x].ch[1];
        }
        return x;
    }
    inline void Flip(int l,int r){
        int t1=select(rt,l),t2=select(rt,r+2);
        splay(t1,0);
        splay(t2,rt);
        t[t[t2].ch[0]].flip^=1;
    }
    inline void inorder(int root){
        if(!root) return;
        pushdown(root);
        inorder(t[root].ch[0]);
        if(t[root].v>0&&t[root].v<=n)
            print(t[root].v);       
        inorder(t[root].ch[1]);
    }
    int build(int l,int r,int f,int p){
        if(l>r) return 0;
        int now=++tot;
        t[now].f=f;
        t[now].p=p;
        t[now].flip=0;
        t[now].v=l+r>>1;
        t[now].ch[0]=build(l,(l+r>>1)-1,now,0);
        t[now].ch[1]=build((l+r>>1)+1,r,now,1);
        upd(now);
        return now;
    }
 
    Splay():rt(0),tot(0){memset(t,0,sizeof t);}
};
Splay tree;
 
int main(){
    read(n);read(m);
    tree.build(0,n+1,0,0);
    tree.rt=1;
    while(m--){
        read(l);read(r);
        tree.Flip(l,r);
    }
    tree.inorder(tree.rt);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值