链接一下题目:luoguP3391[模板]文艺平衡树(Splay)
#include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #include<cstring> #include<iomanip> #include<algorithm> #include<ctime> #include<queue> #include<stack> #define rg register #define lst long long #define N 1000050 using namespace std; int n,m,tot,root; struct Node{ int ch[2]; int v,fa; int size; int lazy; }ljl[N]; inline int read() { rg int s=0,m=1;char ch=getchar(); while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘))ch=getchar(); if(ch==‘-‘)m=0,ch=getchar(); while(ch>=‘0‘&&ch<=‘9‘)s=(s<<3)+(s<<1)+ch-‘0‘,ch=getchar(); return m?s:-s; } inline void Pushup(rg int now) { ljl[now].size=ljl[ljl[now].ch[0]].size+ljl[ljl[now].ch[1]].size+1; } inline void Pushdown(rg int now) { if(ljl[now].lazy) { ljl[ljl[now].ch[0]].lazy^=1; ljl[ljl[now].ch[1]].lazy^=1; swap(ljl[now].ch[0],ljl[now].ch[1]); ljl[now].lazy=0; } } inline void rotate(rg int x) { rg int y=ljl[x].fa,z=ljl[y].fa; rg int k=ljl[y].ch[1]==x; ljl[z].ch[ljl[z].ch[1]==y]=x; ljl[x].fa=z; ljl[y].ch[k]=ljl[x].ch[k^1]; ljl[ljl[x].ch[k^1]].fa=y; ljl[x].ch[k^1]=y; ljl[y].fa=x; Pushup(x),Pushup(y); } inline void splay(rg int x,rg int goal) { while(ljl[x].fa!=goal) { rg int y=ljl[x].fa,z=ljl[y].fa; if(z!=goal)(x==ljl[y].ch[0])^(y==ljl[z].ch[0])?rotate(x):rotate(y); rotate(x); } if(goal==0)root=x; } inline void Insert(rg int x) { int now=root,fa=0; while(now)fa=now,now=ljl[now].ch[x>ljl[now].v]; now=++tot; if(fa)ljl[fa].ch[x>ljl[now].v]=now; ljl[now].ch[0]=ljl[now].ch[1]=0; ljl[now].v=x;ljl[now].fa=fa; ljl[now].size=1; splay(now,0); } inline int Kth(rg int x) { rg int now=root; while(233) { Pushdown(now); if(x>ljl[ljl[now].ch[0]].size+1) x-=ljl[ljl[now].ch[0]].size+1,now=ljl[now].ch[1]; else if(ljl[ljl[now].ch[0]].size>=x)now=ljl[now].ch[0]; else return now; } } inline void Work(rg int le,rg int ri) { rg int qq=Kth(le); rg int hj=Kth(ri+2); splay(qq,0),splay(hj,qq); ljl[ljl[ljl[root].ch[1]].ch[0]].lazy^=1; } void Write(rg int now) { Pushdown(now); if(ljl[now].ch[0])Write(ljl[now].ch[0]); if(ljl[now].v>1&&ljl[now].v<n+2)printf("%d ",ljl[now].v-1); if(ljl[now].ch[1])Write(ljl[now].ch[1]); } int main() { n=read(),m=read(); for(rg int i=1;i<=n+2;++i)Insert(i); for(rg int i=1;i<=m;++i) { rg int le=read(),ri=read(); Work(le,ri); } Write(root); return 0; }