splay-ultra 旋到goal的子节点
int get(int p){
return p==t[t[p].fa].ch[1];
}
void rotate(int x){
int y=t[x].fa;
int z=t[y].fa;
int gx=get(x),gy=get(y);
if(t[x].ch[gx^1])t[t[x].ch[gx^1]].fa=y;
t[y].ch[gx]=t[x].ch[gx^1];
t[x].ch[gx^1]=y;
if(z)t[z].ch[gy]=x;
t[y].fa=x;
t[x].fa=z;
pushup(y);
pushup(x);
}
void splay(int x,int goal){
while(t[x].fa!=goal){
int y=t[x].fa;
if(t[y].fa!=goal){
if(get(y)==get(x)) rotate(y);
else rotate(x);
}
rotate(x);
}
if (!goal) root=x;
}文艺平衡树
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,root=1;
struct node{
int fa,ch[2];
int num;
bool lazytag;
int size;
}t[100010];
void pushdown(int p){
if(t[p].lazytag){
t[t[p].ch[0]].lazytag^=1;
t[t[p].ch[1]].lazytag^=1;
t[p].lazytag=0;
swap(t[p].ch[0],t[p].ch[1]);
}
}
void pushup(int p){
t[p].size=t[t[p].ch[0]].size+t[t[p].ch[1]].size+1;
}
//int newnode(int p){
// t[top].num=p;
// t[top].size=1;
// return top;
//}
//void insert(int &p,int num,int fa){
// if(p==0){
// t[++top].fa=fa;
// p=newnode(num);
// return ;
// }
// if(t[p].num>num){
// //左子树
// insert(t[p].ch[0],num,p);
// }
// if(t[p].num<num){
// //右子树
// insert(t[p].ch[1],num,p);
// }
// pushup(p);
//}
int get(int p){
return p==t[t[p].fa].ch[1];
}
void rotate(int x){
int y=t[x].fa;
int z=t[y].fa;
int gx=get(x),gy=get(y);
if(t[x].ch[gx^1])t[t[x].ch[gx^1]].fa=y;
t[y].ch[gx]=t[x].ch[gx^1];
t[x].ch[gx^1]=y;
if(z)t[z].ch[gy]=x;
t[y].fa=x;
t[x].fa=z;
pushup(y);
pushup(x);
}
void splay(int x,int goal){
while(t[x].fa!=goal){
int y=t[x].fa;
if(t[y].fa!=goal){
if(get(y)==get(x)) rotate(y);
else rotate(x);
}
rotate(x);
}
if (!goal) root=x;
}
void find(int x,int goal){
int p=root;
while(x){
pushdown(p);
if(t[t[p].ch[0]].size>=x){
p=t[p].ch[0];
}else if(t[t[p].ch[0]].size+1==x){
break;
}else{
x-=t[t[p].ch[0]].size+1;
p=t[p].ch[1];
}
}
pushdown(p);
splay(p,goal);
}
void flip(int l,int r){ //r+1到root,l-1旋到root的左子
find(r+2,0);
find(l,root);
t[t[t[root].ch[0]].ch[1]].lazytag^=1;
}
void print(int p){
if(p==0)return ;
pushdown(p);
print(t[p].ch[0]);
if(t[p].num!=0)cout<<t[p].num<<' ';
print(t[p].ch[1]);
}
signed main(){
cin>>n>>m;
t[1].num=0;
t[1].ch[1]=2;
t[1].size=n+2;
for(int i=1;i<=n;i++){
t[i+1].num=i;
t[i+1].ch[1]=i+2;
t[i+1].size=n-i+2;
t[i+1].fa=i;
}
t[n+2].num=0;
t[n+2].fa=n+1;
t[n+2].size=1;
// cout<<endl;
for(int i=1;i<=m;i++){
int l,r;
cin>>l>>r;
flip(l,r);
// print(root);
// cout<<endl;
}
print(root);
return 0;
}