标签:rpo turn 理解 sts 题目 integer else spec sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3913 Accepted Submission(s): 1717
// // main.cpp // hdu1890 // // Created by Candy on 30/11/2016. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define pa t[x].fa #define lc t[x].ch[0] #define rc t[x].ch[1] const int N=1e5+5; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } struct node{ int fa,ch[2],w,size,flp; }t[N]; int root; inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;} inline int wh(int x){return t[pa].ch[1]==x;} inline void pushDown(int x){ if(t[x].flp){ swap(lc,rc); t[lc].flp^=1;t[rc].flp^=1; t[x].flp=0; } } inline void rotate(int x){ int f=t[x].fa,g=t[f].fa,c=wh(x); if(g) t[g].ch[wh(f)]=x;t[x].fa=g; t[f].ch[c]=t[x].ch[c^1];t[t[f].ch[c]].fa=f; t[x].ch[c^1]=f;t[f].fa=x; update(f);update(x); } inline void splay(int x,int tar){ for(;t[x].fa!=tar;rotate(x)) if(t[pa].fa!=tar) rotate(wh(pa)==wh(x)?pa:x); if(tar==0) root=x; } int ne[N]; void spl(int x,int tar){ int _=x; while(x!=tar) ne[pa]=x,x=pa; x=_; for(int i=tar;i!=x;i=ne[i]) pushDown(i); pushDown(x); splay(x,tar); } int build(int l,int r){ if(l>r) return 0; int x=(l+r)>>1; lc=build(l,x-1);rc=build(x+1,r); t[lc].fa=t[rc].fa=x; t[x].w=1;t[x].flp=0; update(x); //printf("build %d %d %d\n",x,lc,rc); return x; } int kth(int k){ int x=root,ls=0; while(x!=0){ pushDown(x); int _=ls+t[lc].size; if(_<k&&k<=_+t[x].w) return x; if(k<=_) x=lc; else ls=_+t[x].w,x=rc; } return -1; } void rever(int l,int r){//printf("rev %d %d ",l,r); splay(kth(l),0); int x=kth(r+2);//printf("x %d\n",x); splay(x,root); t[lc].flp^=1; } int n; struct data{ int id,v; bool operator <(const data &a)const{ if(v==a.v) return id<a.id; return v<a.v; } }a[N]; int main(int argc, const char * argv[]){ while(scanf("%d",&n)!=EOF&&n){ memset(t,0,sizeof(t)); for(int i=1;i<=n;i++) a[i].v=read(),a[i].id=i+1; sort(a+1,a+1+n); root=build(1,n+2); for(int i=1;i<=n;i++){ int x=a[i].id;//printf("hi %d %d\n",i,x); spl(x,0); printf("%d%c",t[lc].size,i<n?‘ ‘:‘\n‘); rever(i,t[lc].size); } } return 0; }
HDU1890 Robotic Sort[splay 序列]
标签:rpo turn 理解 sts 题目 integer else spec sort
原文地址:http://www.cnblogs.com/candy99/p/6120337.html