http://acm.hdu.edu.cn/showproblem.php?pid=3487
每次将要操作的区间收到口袋中 然后进行拆除 插入 翻转 注意拆除节点时 被拆节点的父指针要处理好 不然会影响splay操作时是按一字型还是之子型操作的
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node;
node *null;
struct node
{
node *fa,*ch[2];
int sz,id,rev;
void wipe()
{
fa=ch[0]=ch[1]=null;
sz=1,id=rev=0;
}
void setc(node* tmp,int d)
{
ch[d]=tmp;
tmp->fa=this;
}
int getd()
{
return fa->ch[1]==this;
}
void reverse()
{
if(this==null) return;
swap(ch[0],ch[1]);
rev^=1;
}
void pushup()
{
if(this==null) return;
sz=ch[0]->sz+ch[1]->sz+1;
}
void pushdown()
{
if(this==null) return;
if(rev!=0)
{
ch[0]->reverse();
ch[1]->reverse();
rev=0;
}
}
};
node pool[300010];
node *root,*tail;
int n,q;
void build(node *&cur,int l,int r,node *fa)
{
int m;
if(l>r) return;
m=(l+r)/2;
cur=tail++;
cur->wipe();
cur->fa=fa;
cur->id=m;
build(cur->ch[0],l,m-1,cur);
build(cur->ch[1],m+1,r,cur);
cur->pushup();
}
void init()
{
node *tmp;
tail=pool;
null=tail++;
null->fa=null->ch[0]=null->ch[1]=null;
null->sz=null->id=null->rev=0;
tmp=tail++;
tmp->wipe();
root=tmp;
tmp=tail++;
tmp->wipe();
root->setc(tmp,1);
build(root->ch[1]->ch[0],1,n,root->ch[1]);
root->ch[1]->pushup();
root->pushup();
}
void rotate(node *cur)
{
node *f,*ff;
int c,cc;
f=cur->fa,ff=cur->fa->fa;
f->pushdown();
cur->pushdown();
c=cur->getd(),cc=f->getd();
f->setc(cur->ch[!c],c);
cur->setc(f,!c);
if(ff->ch[cc]==f) ff->setc(cur,cc);
else cur->fa=ff;
f->pushup();
}
void splay(node *&root,node *tar,node *cur)
{
while(cur->fa!=tar)
{
if(cur->fa->fa==tar) rotate(cur);
else
{
cur->fa->fa->pushdown();
cur->fa->pushdown();
cur->pushdown();
if(cur->getd()==cur->fa->getd()) rotate(cur->fa);
else rotate(cur);
rotate(cur);
}
}
cur->pushup();
if(tar==null) root=cur;
}
node *getkth(node *r,int k)
{
node *cur;
cur=r;
cur->pushdown();
while(cur->ch[0]->sz+1!=k)
{
if(cur->ch[0]->sz+1>k)
{
cur=cur->ch[0];
}
else
{
k-=(cur->ch[0]->sz+1);
cur=cur->ch[1];
}
cur->pushdown();
}
return cur;
}
void show()
{
int i;
for(i=1;i<=n+2;i++) printf("%d %d\n",getkth(root,i)->id,getkth(root,i)->sz);
printf("\n");
}
int main()
{
node *ptr;
int i,j,l,r,k;
char op[10];
while(scanf("%d%d",&n,&q)!=EOF)
{
if(n==-1&&q==-1) break;
init();
while(q--)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d%d",&l,&r,&k);
splay(root,null,getkth(root,l));
splay(root,root,getkth(root,r+2));
ptr=root->ch[1]->ch[0];
root->ch[1]->ch[0]=null;
root->ch[1]->pushup();
root->pushup();
splay(root,null,getkth(root,k+1));
splay(root,root,getkth(root,k+2));
root->ch[1]->setc(ptr,0);
root->ch[1]->pushup();
root->pushup();
//show();
}
else
{
scanf("%d%d",&l,&r);
splay(root,null,getkth(root,l));
splay(root,root,getkth(root,r+2));
//show();
root->ch[1]->ch[0]->reverse();
}
}
for(i=2;i<=n+1;i++)
{
ptr=getkth(root,i);
printf("%d",ptr->id);
if(i<n+1) printf(" ");
else printf("\n");
}
}
return 0;
}