仍然是Splay模板题,水化版 1500 。让我们重写一下 1500 的主函数,然后尽情地往下删除该死的第三个操作和第六个询问的恶心代码!!!
需要注意的是,每一次移动光标并不需要将此处Splay到根节点,在进行其他操作时再Splay即可。
【顺便吐槽此题抄袭NOI2003题严重,不仅题目一样,连题目描述的表格甚至每步操作的名称都基本相同~~】
[AHOI2006]文本编辑器editor C++代码实现:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 2100000
int n;
char a[N],b[10];
struct node
{
node *ch[2],*fa;
char v;int s,rev;
inline void pushdown()
{
if(rev)
ch[0]->reverse(),
ch[1]->reverse();
rev=0;
}
inline void maintain()
{
pushdown();
s=ch[0]->s+ch[1]->s+1;
}
inline bool check()
{
return fa->ch[1]==this;
}
inline void combine(node *a,int d)
{
ch[d]=a;
a->fa=this;
}
inline void reverse()
{
rev^=1;
swap(ch[0],ch[1]);
}
};
node *null=new node();
node *root=null;
inline node *newnode(node *f,char val)
{
node *re=new node();
re->s=1;
re->v=val;
re->rev=0;
re->ch[0]=re->ch[1]=null;
re->fa=f;
return re;
}
inline void rotate(node *x,int d)
{
node *k=x->fa;
k->ch[d^1]=x->ch[d];
k->ch[d^1]->fa=k;
x->ch[d]=k;
x->fa=k->fa;
k->fa->ch[k->fa->ch[0]==k?0:1]=x;
k->fa=x;
k->maintain();
x->maintain();
if(root==k) root=x;
}
inline void splay(node *x,node *aim)
{
while(x->fa!=aim)
{
if(x->fa->fa==aim)
rotate(x,x->check()?0:1);
else if(!x->fa->check())
x->check()?rotate(x,0):rotate(x->fa,1),
rotate(x,1);
else
x->check()?rotate(x->fa,0):rotate(x,1),
rotate(x,0);
x->maintain();
}
}
node *build(int l,int r)
{
if(l>r) return null;
int mid=(l+r)>>1;
node *re=newnode(re,a[mid]);
re->combine(build(l,mid-1),0);
re->combine(build(mid+1,r),1);
re->maintain();
return re;
}
void del(node* &x)
{
if(x->ch[0]!=null)
del(x->ch[0]);
if(x->ch[1]!=null)
del(x->ch[1]);
delete x;
}
node *kth(node *x,int k)
{
x->pushdown();
if(k<=x->ch[0]->s)
return kth(x->ch[0],k);
k-=x->ch[0]->s;
if(k==1) return x;
return kth(x->ch[1],k-1);
}
int main()
{
cin>>n;
root=build(0,1);
root->fa=null;
for(int x,t=0,i=1;i<=n;i++)
{
scanf("%s",b);
if(b[0]=='M')
scanf("%d",&t);
else if(b[0]=='I')
{
scanf("%d",&x);
while((a[0]=getchar())=='\n'||a[0]=='\r');
if(x>1) gets(a+1);
splay(kth(root,t+1),null);
splay(kth(root,t+2),root);
root->ch[1]->combine(build(0,x-1),0);
root->ch[1]->maintain();
root->maintain();
}
else if(b[0]=='D')
{
scanf("%d",&x);
splay(kth(root,t+1),null);
splay(kth(root,t+x+2),root);
del(root->ch[1]->ch[0]);
root->ch[1]->ch[0]=null;
root->ch[1]->maintain();
root->maintain();
}
else if(b[0]=='R')
{
scanf("%d",&x);
splay(kth(root,t+1),null);
splay(kth(root,t+x+2),root);
root->ch[1]->ch[0]->reverse();
root->ch[1]->maintain();
root->maintain();
}
else if(b[0]=='G')
{
splay(kth(root,t+2),null);
printf("%c\n",root->v);
}
else if(b[0]=='P') t--;
else t++;
}
}