【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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章