普通:
bool _Start;
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define Tp template<typename T>
#define Ts template<typename T,typename... _T>
Tp il void read(T& x) {
x=0;bool f=0;char c=getchar();
for(;!isdigit(c);c=getchar()) f|=c=='-';
for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
x=(f?-x:x);
}Ts il void read(T& x,_T&... y) {read(x),read(y...);}
const int N=1e5+5;
int n;
struct Splay {
int s[2],fa,siz,key;
}t[N];int rt,idx;
#define ls(x) t[x].s[0]
#define rs(x) t[x].s[1]
#define fa(x) t[x].fa
il int newnode(int x) {t[++idx].key=x,t[idx].siz=1;return idx;}
il void upd(int x) {t[x].siz=t[ls(x)].siz+t[rs(x)].siz+1;}
il void clear(int x) {ls(x)=rs(x)=fa(x)=t[x].siz=t[x].key=0;}
il bool get(int x) {return x==rs(fa(x));}
il void rotate(int x) {
int y=fa(x),z=fa(y),c=get(x);
if(t[x].s[c^1]) fa(t[x].s[c^1])=y;
t[y].s[c]=t[x].s[c^1],t[x].s[c^1]=y,fa(y)=x,fa(x)=z;
if(z) t[z].s[y==t[z].s[1]]=x;
upd(y),upd(x);
}
il void splay(int x) {
for(int f;f=fa(x);rotate(x))
if(fa(f)) rotate(get(f)==get(x)?f:x);
rt=x;
}
il void ins(int x) {
int p=rt,f=0;
while(p) {
f=p;
p=t[p].s[x>t[p].key];
}
p=newnode(x),fa(p)=f,t[f].s[x>t[f].key]=p;
splay(p);
}
il void del(int x) {
int p=rt,f=0;
while(p&&t[p].key!=x) {
f=p;
p=t[p].s[x>t[p].key];
}
if(!p) {
splay(f);
return ;
}
splay(p);int cur=ls(p);
if(!cur) {
rt=rs(p),fa(rt)=0,clear(p);
return ;
}
while(rs(cur)) cur=rs(cur);
rs(cur)=rs(p),fa(rs(p))=cur,fa(ls(p))=0,clear(p);
upd(cur),splay(cur);
}
il int rnk(int x) {
int ans=1,p=rt,f;
while(p) {
f=p;
if(t[p].key<x) ans+=t[ls(p)].siz+1,p=rs(p);
else p=ls(p);
}
return splay(f),ans;
}
il int kth(int k) {
int p=rt;
while(p) {
int siz=t[ls(p)].siz+1;
if(siz==k) break;
if(siz>k) p=ls(p);
else k-=siz,p=rs(p);
}
return splay(p),t[p].key;
}
il int pre(int x) {
int p=rt,ans,f;
while(p) {
f=p;
if(t[p].key>=x) p=ls(p);
else ans=t[p].key,p=rs(p);
}
return splay(f),ans;
}
il int suc(int x) {
int p=rt,ans,f;
while(p) {
f=p;
if(t[p].key<=x) p=rs(p);
else ans=t[p].key,p=ls(p);
}
return splay(f),ans;
}
bool _End;
int main() {
fprintf(stderr,"Memory: %.4lf Mib\n",abs(&_End-&_Start)/1048576.0);
read(n);
for(int i=1;i<=n;i++) {
int op,x;read(op,x);
if(op==1) ins(x);
if(op==2) del(x);
if(op==3) printf("%d\n",rnk(x));
if(op==4) printf("%d\n",kth(x));
if(op==5) printf("%d\n",pre(x));
if(op==6) printf("%d\n",suc(x));
}
return 0;
}
文艺:
bool _Start;
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define Tp template<typename T>
#define Ts template<typename T,typename... _T>
Tp il void read(T& x) {
x=0;bool f=0;char c=getchar();
for(;!isdigit(c);c=getchar()) f|=c=='-';
for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
x=(f?-x:x);
}Ts il void read(T& x,_T&... y) {read(x),read(y...);}
const int N=1e5+5;
int n,m;
int a[N];
struct Splay {
int s[2],fa,siz,key,lz;
}t[N];int idx,rt;
#define ls(x) t[x].s[0]
#define rs(x) t[x].s[1]
#define fa(x) t[x].fa
il int newid(int x) {t[++idx].key=x,t[idx].siz=1;return idx;}
il void upd(int x) {t[x].siz=t[ls(x)].siz+t[rs(x)].siz+1;}
il bool get(int x) {return x==t[fa(x)].s[1];}
il void pushdown(int x) {
if(!t[x].lz) return ;
swap(ls(x),rs(x));
t[ls(x)].lz^=1,t[rs(x)].lz^=1;
t[x].lz=0;
}
il void rotate(int x) {
int y=fa(x),z=fa(y),c=get(x);
if(t[x].s[c^1]) fa(t[x].s[c^1])=y;
t[y].s[c]=t[x].s[c^1],t[x].s[c^1]=y,fa(y)=x,fa(x)=z;
if(z) t[z].s[y==t[z].s[1]]=x;
upd(y),upd(x);
}
il void splay(int x,int y) {
for(int f;(f=fa(x))!=y;rotate(x))
if(fa(f)!=y) rotate(get(f)==get(x)?f:x);
if(!y) rt=x;
}
il int bt(int l,int r) {
if(l>r) return 0;
int p=++idx,mid=(l+r)>>1;
t[p].key=mid,t[p].siz=1;
ls(p)=bt(l,mid-1);
rs(p)=bt(mid+1,r);
fa(ls(p))=fa(rs(p))=p;
upd(p);
return p;
}
il void ins(int x) {
int p=rt,f=0;
while(p) {
f=p;
p=t[p].s[x>t[p].key];
}
p=newid(x),fa(p)=f,t[f].s[x>t[f].key]=p;
splay(p,0);
}
il int kth(int x) {
int p=rt;x++;
while(p) {
pushdown(p);
int siz=t[ls(p)].siz+1;
if(siz==x) return p;
else if(siz>x) p=ls(p);
else x-=siz,p=rs(p);
}
}
il void myreverse(int l,int r) {
int x=kth(l-1),y=kth(r+1);
splay(x,0),splay(y,x);
t[ls(y)].lz^=1;
}
void traversal(int x) {
pushdown(x);
if(ls(x)) traversal(ls(x));
if(t[x].key>0&&t[x].key<=n) printf("%d ",t[x].key);
if(rs(x)) traversal(rs(x));
}
bool _End;
int main() {
fprintf(stderr,"Memory: %.4lf Mib\n",abs(&_End-&_Start)/1048576.0);
read(n,m);
rt=bt(0,n+1);
while(m--) {
int l,r;read(l,r);
myreverse(l,r);
}
traversal(rt);
return 0;
}
标签:cur,rs,int,板子,Splay,il,key,ls
From: https://www.cnblogs.com/kbzcz/p/18124029/splay