https://www.lydsy.com/JudgeOnline/problem.php?id=3223
this is the first time I get in touch with the SplayTree
An miraculous Data Structure
For this problem
you should use the rotate
to make the intervals into a self-tree
and add a lazy lable to rotate the deep of the tree one by one
for the first time
I can't write this complex Data Structure by myself
so I use for reference from the VJ
code of AC:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int fa[N],l[N],r[N],num[N],rev[N];
int root=0;
void pushdown(int p){
if(rev[p]){
swap(l[p],r[p]);
rev[l[p]]^=1;
rev[r[p]]^=1;
rev[p]^=1;
}
}
void update(int p){
num[p]=num[l[p]]+num[r[p]]+1;
}
void rotate(int x,int &k){
int y=fa[x];int z=fa[y];
int R=(r[y]==x);
if(y==k) k=x;
else{
if(l[z]==y) l[z]=x;
else r[z]=x;
}
if(R){
fa[x]=z;fa[y]=x;fa[l[x]]=y;
r[y]=l[x];
l[x]=y;
}else{
fa[x]=z;fa[y]=x;fa[r[x]]=y;
l[y]=r[x];
r[x]=y;
}
update(x);
update(y);
}
void splay(int x,int &k){
while(x!=k){
int y=fa[x];
int z=fa[y];
if(y!=k){
if((x==l[y])^(y==l[z])) rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int find(int p,int x){
pushdown(p);
int L=l[p],R=r[p];
if(num[L]+1==x){
// splay(x,root);
return p;
}
if(num[l[p]]>=x) return find(l[p],x);
else return find(r[p],x-1-num[l[p]]);
}
void reverse(int L,int R){
int x=find(root,L);
int y=find(root,R+2);
splay(x,root);
splay(y,r[x]);
rev[l[y]]^=1;
}
void Insert(int x){
fa[root]=x;
l[x]=root;
num[x]=num[root]+1;
root=x;
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n+2;++i){
Insert(i);
}
while(m--){
int a,b;
cin>>a>>b;
reverse(a,b);
}
for(int i=1;i<=n;++i){
cout<<find(root,i+1)-1<<" ";
}
cout<<endl;
}