bzoj 3223
来源:互联网 发布:淘宝助理6.0官方下载 编辑:程序博客网 时间:2024/06/06 04:11
splay
#include<bits/stdc++.h>using namespace std;const int maxn=100005;int ch[maxn][2],fa[maxn],vis[maxn],val[maxn],root=0,tot=0,s[maxn],n,m;inline void down(int x){ if(vis[x]){ int k1=ch[x][0],k2=ch[x][1]; swap(ch[k1][0],ch[k1][1]);vis[k1]^=1; swap(ch[k2][0],ch[k2][1]);vis[k2]^=1; }vis[x]=0;}inline void up(int x){s[x]=s[ch[x][0]]+s[ch[x][1]]+1;}void rotate(int x,int d){ int y=fa[x];down(y);down(x); ch[y][d^1]=ch[x][d]; if(ch[x][d])fa[ch[x][d]]=y; if(fa[y])ch[fa[y]][ch[fa[y]][1]==y]=x; fa[x]=fa[y]; ch[x][d]=y; fa[y]=x; up(y); }void splay(int x,int goal){ down(x); while(fa[x]!=goal){ if(fa[fa[x]]==goal)rotate(x,ch[fa[x]][0]==x); else{ int y=fa[x]; int d=ch[fa[y]][0]==y; if(ch[y][d]==x){ rotate(x,d^1); rotate(x,d); } else{ rotate(y,d); rotate(x,d); } } }up(x); if(goal==0)root=x;}inline int pos(int x,int p){ while(x){down(x); /// int ss=s[ch[x][0]]+1; if(p==ss)return x; if(p>ss)x=ch[x][1],p-=ss; else x=ch[x][0]; }}void build(int& x,int l,int r,int f){ if(l>r)return; int mid=(l+r)>>1; x=++tot; fa[x]=f; val[x]=mid;s[x]=1; build(ch[x][0],l,mid-1,x); build(ch[x][1],mid+1,r,x); up(x);}void print(int x){ if(!x)return; down(x); print(ch[x][0]); if(val[x]!=0 && val[x]!=n+1) printf("%d ",val[x]); print(ch[x][1]);}int main(){//freopen("in.in","r",stdin);//freopen("out.out","w",stdout); scanf("%d%d",&n,&m); build(root,0,n+1,0);int l,r; for(int i=1;i<=m;i++){ scanf("%d%d",&l,&r);l++,r++; int x=pos(root,l-1); splay(x,0); int y=pos(root,r+1); splay(y,root); int v=ch[y][0];swap(ch[v][0],ch[v][1]); vis[v]^=1; } print(root);printf("\n");}
0 0