正题
题目链接:https://ac.nowcoder.com/acm/contest/7865/G
题目大意
nnn个数从1∼n1\sim n1∼n,mmm次拿出其中一段放到头部,求最终序列。
解题思路
用SplaySplaySplay拿出一段区间然后丢到头部就好了。
时间复杂度O(nlogn)O(n\log n)O(nlogn)
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int n,m,root,siz[N],fa[N],val[N],t[N][2];
void PushUp(int x)
{siz[x]=siz[t[x][0]]+siz[t[x][1]]+1;return;}
bool Direct(int x)
{return t[fa[x]][1]==x;}
void Connect(int x,int y,int dir)
{t[x][dir]=y;fa[y]=x;return;}
void Rotate(int x){int y=fa[x],z=fa[fa[x]];int xs=Direct(x),ys=Direct(y);Connect(y,t[x][xs^1],xs);Connect(x,y,xs^1);Connect(z,x,ys);PushUp(y);PushUp(x); return;
}
void Splay(int x,int f){while(fa[x]!=f){int up=fa[x];if(fa[up]==f)Rotate(x);else if(Direct(x)==Direct(up))Rotate(up),Rotate(x);else Rotate(x),Rotate(x);}if(!f)root=x;return;
}
int Find(int x,int k){if(siz[t[x][0]]>=k)return Find(t[x][0],k);if(siz[t[x][0]]+1==k)return x;return Find(t[x][1],k-siz[t[x][0]]-1);
}
void Write(int x){if(!x)return;Write(t[x][0]);if(val[x])printf("%d ",val[x]);Write(t[x][1]);
}
int main()
{scanf("%d%d",&n,&m);siz[1]=1;for(int i=2;i<=n+1;i++){val[i]=i-1;fa[i-1]=i;t[i][0]=i-1;PushUp(i);}t[n+2][0]=n+1;fa[n+1]=n+2;PushUp(n+2);root=n+2; for(int i=1;i<=m;i++){int l,r,x,y,tmp;scanf("%d%d",&l,&r);r=l+r-1;x=Find(root,l);y=Find(root,r+2);Splay(x,0);Splay(y,x);tmp=t[y][0];t[y][0]=0;fa[tmp]=0;PushUp(y);x=Find(root,1);y=Find(root,2);Splay(x,0);Splay(y,x);t[y][0]=tmp;fa[tmp]=y;Splay(tmp,0);root=tmp;}Write(root);
}