BZOJ_1507_Editor_[NOI2003]_(Splay)

时间:2023-03-08 21:24:43
BZOJ_1507_Editor_[NOI2003]_(Splay)

描述


http://www.lydsy.com/JudgeOnline/problem.php?id=1507

简单区间操作的模板题

1507: [NOI2003]Editor

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 3092  Solved: 1244
[Submit][Status][Discuss]

Description

BZOJ_1507_Editor_[NOI2003]_(Splay)

Input


入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作。其中:
为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它们(如果难以理解这句话,可以参考样例)。
除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。 这里我们有如下假定: 
MOVE操作不超过50000个,INSERT和DELETE操作的总个数不超过4000,PREV和NEXT操作的总个数不超过200000。 
所有INSERT插入的字符数之和不超过2M(1M=1024*1024),正确的输出文件长度不超过3M字节。 
DELETE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作必然不会试图把光标移动到非法位置。 
输入文件没有错误。 对C++选手的提示:经测试,最大的测试数据使用fstream进行输入有可能会比使用stdio慢约1秒。

Output

输出文件editor.out的每行依次对应输入文件中每条GET指令的输出。

Sample Input

15
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 15
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22

Sample Output

.\/.
abcde^_^f.\/.ghijklmno

HINT

Source

分析


和BZOJ_1269很像,而且更简单.

首先加入一个起始字符和尾字符(因为我们解决问题的办法都是在两个区间之间进行的).

操作:

1.插入:把at转到根节点,把at+1转到根节点的右儿子,然后在at+1的左儿子处插入即可.

2.移动:直接输入到at即可.

3.删除:把at转到根节点,把at+n+1转到根节点的右儿子,然后把at+n+1的左儿子直接改成null即可.

4.&5.移动光标就at--,at++即可.

 #include <cstdio>
#include <algorithm>
using namespace std; const int maxn=(<<)+,oo=~0u>>; int n,x,at,cur;
char str[maxn],s[]; struct Splay{
struct node{
node* ch[],* pa;
char v; int s;
node(int v,node* t):v(v){ ch[]=ch[]=pa=t; s=; }
bool d(){ return pa->ch[]==this; }
void setc(node* t,bool d){ ch[d]=t; t->pa=this; }
void push_up(){ s=ch[]->s+ch[]->s+; }
}*root,*null;
Splay(){
null=new node('\0',NULL); null->s=;
root=new node('\0',null);
node* t=new node('\0',null);
root->setc(t,);
root->push_up();
}
void rot(node* o){
node* pa=o->pa; bool d=o->d();
pa->pa->setc(o,pa->d());
pa->setc(o->ch[!d],d);
o->setc(pa,!d);
pa->push_up();
if(pa==root) root=o;
}
void splay(node* o,node* pa){
while(o->pa!=pa){
if(o->pa->pa==pa) rot(o);
else o->d()==o->pa->d()?(rot(o->pa),rot(o)):(rot(o),rot(o));
}
o->push_up();
}
node* kth(int k){
k++;
for(node* t=root;;){
int s=t->ch[]->s+;
if(s==k) return t;
if(k>s) k-=s,t=t->ch[];
else t=t->ch[];
}
}
node* build(int l,int r){
if(l==r) return new node(str[l],null);
if(l>r) return null;
int m=l+(r-l)/;
node* t=new node(str[m],null);
t->setc(build(l,m-),);
t->setc(build(m+,r),);
t->push_up();
return t;
}
node* find(int l,int r){
node* L=kth(l); splay(L,null);
node* R=kth(r); splay(R,L);
return R;
}
void insert(int at,int cur){
node* t=find(at,at+);
t->setc(build(,cur),); t->push_up();
splay(t,null);
}
void remove(int at,int n){
node* t=find(at,at+n+);
t->setc(null,); t->push_up();
splay(t,null);
}
void print(node* o){
if(o==null) return;
print(o->ch[]);
printf("%c",o->v);
print(o->ch[]);
}
void print(int at,int n){
node* t=find(at,at+n+);
print(t->ch[]);
printf("\n");
}
}tree; int main(){
scanf("%d",&n);
while(n--){
scanf("%s",s);
if(s[]=='I'){
cur=;
scanf("%d",&x);
while(x--){
while(str[cur]=getchar(),str[cur]=='\n');
cur++;
}
cur--;
tree.insert(at,cur);
}
else if(s[]=='M') scanf("%d",&at);
else if(s[]=='D'){
scanf("%d",&x);
tree.remove(at,x);
}
else if(s[]=='G'){
scanf("%d",&x);
tree.print(at,x);
}
else if(s[]=='P') at--;
else at++;
}
return ;
}