2020牛客国庆集训派对day8G-Shuffle Cards【Splay】
生活随笔
收集整理的這篇文章主要介紹了
2020牛客国庆集训派对day8G-Shuffle Cards【Splay】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
正題
題目鏈接:https://ac.nowcoder.com/acm/contest/7865/G
題目大意
nnn個(gè)數(shù)從1~n1\sim n1~n,mmm次拿出其中一段放到頭部,求最終序列。
解題思路
用SplaySplaySplay拿出一段區(qū)間然后丟到頭部就好了。
時(shí)間復(fù)雜度O(nlog?n)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); }總結(jié)
以上是生活随笔為你收集整理的2020牛客国庆集训派对day8G-Shuffle Cards【Splay】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 逶迤怎么读 逶迤的意思介绍
- 下一篇: P4852-yyf hates chou