BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

1269: [AHOI2006]文本编辑器editor

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 4633  Solved: 1782
[Submit][Status][Discuss]

Description

这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。光标:在一段文本中用于指示位置的标记,可以位于文本的第一个字符之前,文本的最后一个字符之后或文本的某两个相邻字符之间。文本编辑器:为一个可以对一段文本和该文本中的一个光标进行如下七条操作的程序。如果这段文本为空,我们就说这个文本编辑器是空的。 编写一个程序: 建立一个空的文本编辑器。 从输入文件中读入一些操作指令并执行。 对所有执行过的GET操作,将指定的内容写入输出文件。

Input

输入文件中第一行是指令条数N,以下是需要执行的N个操作。除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。

Output

依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。

Sample Input

10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
editor
Move 0
Get
Move 11
Rotate 4
Get

Sample Output

B
t

HINT

对输入数据我们有如下假定: MOVE操作不超过50 000个,INSERT、DELETE和ROTATE操作作的总个数不超过6 000,GET操作不超过20 000个,PREV和NEXT操作的总个数不超过20 000。 所有INSERT插入的字符数之和不超过2M(1M=1 024*1 024)。 DELETE操作、ROTATE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作不会把光标移动到非法位置。 输入文件没有错误。

Source


Splay大板题2333

可惜没有1A,一开始我维护光标在根节点,T得惨烈

没有想到直接记录下光标的位置,要操作再翻上来= =

写这种题最舒服了^ ^

我精简代码的功夫又上了一层【逃

82行splay搞定

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define isr(u) (e[e[u].f].ch[1] == u)
#define ls e[u].ch[0]
#define rs e[u].ch[1]
#define sizl(u) (e[e[u].ch[0]].siz)
using namespace std;
const int maxn = 100005,maxm = 3000005,INF = 1000000000;
char cmd[15],s[maxm];
int N,siz = 0,rt = 0,pos = 1;
struct node{char c; int siz,ch[2],f,rev;}e[maxm];
inline void pup(int u){e[u].siz = e[ls].siz + 1 + e[rs].siz;}
inline void pd(int u){if (e[u].rev) ls ^= rs ^= ls ^= rs,e[ls].rev ^= 1,e[rs].rev ^= 1,e[u].rev ^= 1;}
inline void push_down(int u){if (e[u].f) push_down(e[u].f); pd(u);}
inline void spin(int u){
	int s = isr(u),fa = e[u].f;
	e[u].f = e[fa].f; if (e[fa].f) e[e[fa].f].ch[isr(fa)] = u;
	e[fa].ch[s] = e[u].ch[s ^ 1]; if (e[u].ch[s^ 1]) e[e[u].ch[s^ 1]].f = fa;
	e[fa].f = u; e[u].ch[s ^ 1] = fa;
	pup(fa); pup(u);
}
inline void splay(int u,int v = 0){
	for (push_down(u); e[u].f != v; spin(u))
		if (e[e[u].f].f != v) spin((isr(u) ^ isr(e[u].f)) ? u : e[u].f);
	if (!v) rt = u;
}
void Kth(int u,int k,int p){
	while (true){
		pd(u);
		if (sizl(u) >= k) u = ls;
		else if (sizl(u) + 1 == k) {splay(u,p);break;}
		else k -= sizl(u) + 1,u = rs;
	}
}
int build(int l,int r,int fa){
	if (l > r) return 0;
	int u = ++siz,mid = l + r >> 1;
	e[u].siz = 1; e[u].f = fa; e[u].c = s[mid];
	e[u].ch[0] = build(l,mid - 1,u);
	e[u].ch[1] = build(mid + 1,r,u);
	pup(u); return u;
}
void Move(){scanf("%d",&pos);pos++;}
void Insert(){
	int len; scanf("%d",&len);
	char c = getchar(); while (c < 32 || c > 126) c = getchar();
	for (int i = 1; i <= len; i++) s[i] = c,c = getchar();
	Kth(rt,pos,0); Kth(rt,pos + 1,rt);
	int RT = build(1,len,0),u = e[rt].ch[1];
	e[u].ch[0] = RT; e[RT].f = u; pup(u); pup(rt);
}
void Delete(){
	int k; scanf("%d",&k);
	Kth(rt,pos,0); Kth(rt,pos + k + 1,rt);
	e[e[rt].ch[1]].ch[0] = 0; pup(e[rt].ch[1]); pup(rt);
}
void Rotate(){
	int k; scanf("%d",&k);
	Kth(rt,pos,0); Kth(rt,pos + k + 1,rt);
	e[e[e[rt].ch[1]].ch[0]].rev ^= 1;
}
void Get(){Kth(rt,pos + 1,0); printf("%c\n",e[rt].c);}
int main(){
	e[rt = ++siz].ch[1] = ++siz; e[siz].f = rt; e[rt].siz = 2; e[siz].siz = 1;
	scanf("%d",&N);
	while (N--){
		scanf("%s",cmd);
		switch (cmd[0]){
			case 'M':Move();break;
			case 'I':Insert();break;
			case 'D':Delete();break;
			case 'R':Rotate();break;
			case 'G':Get();break;
			case 'P':pos--;break;
			case 'N':pos++;break;
			default:break;
		}
	}
	return 0;
}


posted @ 2017-12-03 13:58  Mychael  阅读(151)  评论(0编辑  收藏  举报