#include<bits/stdc++.h>
using namespace std;
struct Node{
Node *ch[2],*fa;
int x,siz;bool rev;
Node(int);
void maintain(){siz=ch[0]->siz+ch[1]->siz+1;}
int dir(){
if(fa->ch[0]==this) return 0;
if(fa->ch[1]==this) return 1;
return -1;
}
void pushdown();
}*null,*root;
Node :: Node(int _){
x=_;
ch[0]=ch[1]=fa=null;
siz=1;
rev=0;
}
inline void Node :: pushdown(){
if(!rev) return;
swap(ch[0],ch[1]);
rev^=1;
if(ch[0]!=null) ch[0]->rev^=1;
if(ch[1]!=null) ch[1]->rev^=1;
}
void Build(Node *father,Node *&x,int l,int r){
if(l>r) return;
int mid=l+r>>1;
x=new Node(mid);
x->fa=father;
Build(x,x->ch[0],l,mid-1);Build(x,x->ch[1],mid+1,r);
x->maintain();
}
Node *Kth(Node *x,int k){
x->pushdown();
if(k<=x->ch[0]->siz) return Kth(x->ch[0],k);
if(k==x->ch[0]->siz+1) return x;
return Kth(x->ch[1],k-x->ch[0]->siz-1);
}
inline void Rotate(Node *o,int b){
Node *x=o->ch[b^1];
o->ch[b^1]=x->ch[b]; x->ch[b]->fa=o;
x->ch[b]=o; o->fa->ch[o->dir()]=x;
x->fa=o->fa;o->fa=x;
o->maintain();x->maintain();
}
inline void Splay(Node *x,Node *y){
while(x->fa!=y){
if(x->fa->fa!=y){
if(x->dir()==x->fa->dir()) Rotate(x->fa->fa,x->dir()^1);
}
Rotate(x->fa,x->dir()^1);
}
if(y==null) root=x;
}
inline void Reverse(int L,int R){
if(L==R) return;
Node *a=Kth(root,L); Splay(a,null);
Node *b=Kth(root,R+2); Splay(b,root);
root->ch[1]->ch[0]->rev^=1;
}
inline void init(){
null=new Node(-1);
null->siz=0;null->rev=0;
root=null;
}
int main(){
init();
int n,m;
scanf("%d%d",&n,&m);
Build(null,root,0,n+1);
for(int i=1;i<=m;++i){
int L,R;
scanf("%d%d",&L,&R);
Reverse(L,R);
}
for(int i=2;i<=n+1;++i) printf("%d ",Kth(root,i)->x);
return 0;
}
【模板】Splay
最新推荐文章于 2022-03-06 15:21:18 发布