[模板]splay
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int dir(int x){return s[f[x]][1]==x;} 2 void rotate(int x){ 3 int fa=f[x],gr=f[fa],di=dir(x),son=s[x][di^1]; 4 s[fa][di]=son;f[son]=fa; 5 s[gr][dir(fa)]=x;f[x]=gr; 6 s[x][di^1]=fa;f[fa]=x; 7 } 8 void splay(int x,int goal=0){ 9 while(f[x]!=goal){ 10 int fa=f[x],gr=f[fa]; 11 if(gr!=goal)if(dir(fa)==dir(x))rotate(fa); 12 else rotate(x); 13 rotate(x); 14 } 15 if(!goal)root=x; 16 } 17 void fly(int x,int p=root){ 18 while(x!=v[p]&&s[p][x>v[p]])p=s[p][x>v[p]]; 19 splay(p); 20 } 21 void insert(int x,int p=root,int fa=0){ 22 while(p&&v[p]!=x)fa=p,p=s[p][x>v[p]]; 23 if(p)tim[p]++; 24 else p=++cnt,s[fa][x>v[fa]]=p,f[p]=fa,v[p]=x,tim[p]=1; 25 splay(p); 26 } 27 void remove(int x){ 28 fly(x);if(tim[root]>1)tim[root]--; 29 else { 30 int rc=s[root][1];fly(x-1); 31 s[root][1]=rc;f[rc]=root; 32 } 33 } 34 int pre(int x){ 35 fly(x);if(v[root]<x)return root; 36 int p=s[root][0]; 37 while(s[p][1])p=s[p][1]; 38 return p; 39 } 40 int suc(int x){ 41 fly(x);if(v[root]>x)return root; 42 int p=s[root][1]; 43 while(s[p][0])p=s[p][0]; 44 return p; 45 }
$Fate \ is \ Fake$