又是一道splay题 ,写splay的速度的确快了不少 ,再接再厉;
1.
int d= u==f->ch[1];
push_down(f);
push_down(u);
这里好像前后都无所谓,好悬=玄
2,居然忘了在 findk的时候下方标记我也是心累,蜜汁re就是结果,下要细心啊
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ls u->ch[0]
#define rs u->ch[1]
using namespace std;
int n,m;
struct Tree{
int v,size,flag;
Tree* ch[3],*f;
Tree(Tree* ll,Tree* rr,Tree* ff,int vv){
size=1;v=vv;
flag=0;
ch[0]=ll;ch[1]=rr;f=ff;
}
};
void push_up(Tree* u){
u->size=1;
if(ls!=NULL)u->size+=ls->size;
if(rs!=NULL)u->size+=rs->size;
}
void push_down(Tree* u){
if(!u->flag)return;
swap(ls,rs);
if(ls!=NULL)ls->flag=!ls->flag;
if(rs!=NULL)rs->flag=!rs->flag;
u->flag=0;
}
void Rotate(Tree* u){
if(u->f==NULL)return ;
Tree* f=u->f;Tree* ff=f->f;
int d= u==f->ch[1];
push_down(f);
push_down(u);
if(u->ch[!d]!=NULL)u->ch[!d]->f=f;
f->ch[d]=u->ch[!d];
u->f=ff;
if(ff!=NULL)ff->ch[f==ff->ch[1]]=u;
u->ch[!d]=f;
f->f=u;
push_up(f);
push_up(u);
}
Tree* root;
void Splay(Tree* u){
while(u->f!=NULL){
Tree* f=u->f;Tree* ff=f->f;
if(ff==NULL){
Rotate(u);
root = u;
return;
}
int d = u== f->ch[1];
int dd= f==ff->ch[1];
if(d==dd) Rotate(f);
else Rotate(u);
Rotate(u);
}
root=u;
}
Tree* build(int l,int r,Tree* f){
if(l==r) return new Tree(NULL,NULL,f,l);
if(l==r-1){
Tree* rr=new Tree(NULL,NULL,f,r);
rr->ch[0]=new Tree(NULL,NULL,rr,l);
rr->size=2;
return rr;
}
int mid=(l+r)>>1;
Tree* rv=new Tree(NULL,NULL,f,mid);
rv->ch[0]=build(l,mid-1,rv);
rv->ch[1]=build(mid+1,r,rv);
push_up(rv);
return rv;
}
Tree* findk(Tree* u,int k){
push_down(u);
int lk=0;
if(ls!=NULL)lk=ls->size;
if(k==lk+1)return u;
if(k<=lk)return findk(ls,k);
return findk(rs,k-lk-1);
}
void Print(Tree* u){
push_down(u);
if(ls!=NULL)Print(ls);
printf("%d ",u->v);
if(rs!=NULL)Print(rs);
}
int main(){
scanf("%d%d",&n,&m);
root=build(1,n,NULL);
//Print(root);
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
Tree* right,*left;
Tree* ll=findk(root,x);
Tree* rr=findk(root,y);
Splay(ll);
left=root->ch[0];root->ch[0]=NULL;if(left!=NULL)left->f=NULL;
push_up(root);
Splay(rr);
/*Print(root);
printf("dsad\n");*/
right=root->ch[1];root->ch[1]=NULL;if(right!=NULL)right->f=NULL;
push_up(root);
root->flag=!root->flag;
Splay(findk(root,1));
root->ch[0]=left;if(left!=NULL)left->f=root;
push_up(root);
Splay(findk(root,y));
root->ch[1]=right;if(right!=NULL)right->f=root;
push_up(root);
}
Print(root);
return 0;
}