【bzoj1269】【AHOI2006】文本編輯器editor【Splay】

被文藝平衡樹折磨了一天以後發現這道題就很好做啦~~~

傳送門:http://www.lydsy.com/JudgeOnline/problem.php?id=1269
但是c++喜聞樂見的gets十分不好使。

如果insert的字符串有空格,gets會跳過去不讀空格。

好在題目給了ASCII碼的範圍。

這樣就可做了。
寫的時候不要把功能一次性全寫完,先寫個基本的,排除一下低級錯誤……我把Move和insert寫完以後發現Splay又出了幾個沙茶錯誤- -

還有居然把Next和Prev搞反了- -……

Splay中cur代表當前光標位置。

flip是標記是否反轉的標記~

p表示當前節點是他爸的左孩子還是右孩子。

build是用來給字符串建樹,建完之後把這棵樹的樹根直接掛到上面去~(預先把光標兩側的節點Splay到一塊去)。

代碼在下面。

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
char str[3000002],cmd[10];
int n,x;
struct Splay{
	static const int maxn=3000001;
	struct node{
		int ch[2],f,s;
		char v;
		bool p,flip;
	}t[maxn];
	int rt,tot,cur;
	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;
		swap(t[x].ch[0],t[x].ch[1]);
		t[x].flip=0;
	}
	inline void sc(int a,int b,bool c){//把節點a的c孩子置爲b(c=0:左孩子,c=1:右孩子)
		t[a].ch[c]=b;
		t[b].f=a;
		t[b].p=c;
	}
	inline void rot(int x){
		int y=t[x].f,p=t[x].p;
		if(y==rt) t[rt=x].f=0;
		else sc(t[y].f,x,t[y].p);
		sc(y,t[x].ch[!p],p);
		sc(x,y,!p);
		upd(y);
	}
	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);
//			cout<<x<<"\n";
		}
		upd(x);
	}
	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=false;
		t[now].v=str[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;
	}
	int select(int x,int k){
		while(1){
			pushdown(x);
			if(k==t[t[x].ch[0]].s+1)return x;
			else 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];
		}
	}
	inline int insert(int subtree){
		splay(select(rt,cur+1),0);
		splay(select(rt,cur+2),rt);
		sc(t[rt].ch[1],subtree,0);
		upd(t[rt].ch[1]);
		upd(rt);
	}
	inline int Flip(int length){
		splay(select(rt,cur+1),0);
		splay(select(rt,cur+2+length),rt);
		t[t[t[rt].ch[1]].ch[0]].flip^=1;
	}
	inline void del(int length){
		splay(select(rt,cur+1),0);
		splay(select(rt,cur+2+length),rt);
		sc(t[rt].ch[1],0,0);
		upd(t[rt].ch[1]);
		upd(rt);
	}
	inline char get(){
		return t[select(rt,cur+2)].v;
	}
	Splay():rt(0),tot(0),cur(0){}
}editor;
int main(){
	scanf("%d\n",&n);
	editor.rt=editor.build(0,1,0,0);
	while(n--){
		scanf("%s",cmd);
		switch(cmd[0]){
			case 'M':
				scanf("%d",&x);
				editor.cur=x;
				break;
			case 'I':
				scanf("%d",&x);
				do{str[0]=getchar();}while(str[0]<32||str[0]>126);
				for(int i=1;i<x;++i) str[i]=getchar();
				getchar();
				editor.insert(editor.build(0,x-1,0,0));
				break;
			case 'D':
				scanf("%d",&x);
				editor.del(x);
				break;
			case 'R':
				scanf("%d",&x);
				editor.Flip(x);
				break;
			case 'G':
				putchar(editor.get());
				putchar('\n');
				break;
			case 'P':
				editor.cur--;
				break;
			case 'N':
				editor.cur++;
				break;
		}
	}
	return 0;
}

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