hdu 1890 Robotic Sort http://acm.hdu.edu.cn/showproblem.php?pid=1890 2012-12-7
区间反转
View Code
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int N=100010; 6 int num[N],rnk[N],pos[N]; 7 bool cmp(int a,int b) 8 { 9 return num[a]<num[b] || (num[a]==num[b] && a<b); 10 } 11 12 struct SplayTree 13 { 14 int ch[N][2],sz[N]; 15 int val[N],minv[N],rev[N]; 16 int root,tot; 17 void rotate(int &rt,int d) 18 { 19 int p=ch[rt][d^1]; 20 ch[rt][d^1]=ch[p][d]; 21 ch[p][d]=rt; 22 maintain(rt); 23 maintain(p); 24 rt=p; 25 } 26 void splay(int &rt,int k)//k从1开始 27 { 28 pushdown(rt); 29 if(k==sz[ch[rt][0]]+1) return; 30 int d=k>sz[ch[rt][0]]+1; 31 if(d) k-=sz[ch[rt][0]]+1; 32 int p=ch[rt][d]; 33 pushdown(p); 34 if(k!=sz[ch[p][0]]+1) 35 { 36 int d2=k>sz[ch[p][0]]+1; 37 int k2=(d2==0?k:k-sz[ch[p][0]]-1); 38 splay(ch[p][d2],k2); 39 if(d==d2) rotate(rt,d^1); 40 else rotate(ch[rt][d],d); 41 } 42 rotate(rt,d^1); 43 //shownode(rt); 44 } 45 int merge(int left,int right)//right可以为0,left不行 46 { 47 splay(left,sz[left]); 48 ch[left][1]=right; 49 maintain(left); 50 return left; 51 } 52 void debug(){printf("%d\n",root); travel(root);} 53 void shownode(int p) 54 { 55 printf("o:%d l:%d r:%d sz:%d minv:%d val:%d rev:%d\n",p,ch[p][0],ch[p][1],sz[p],minv[p],val[p],rev[p]); 56 } 57 void travel(int p) 58 { 59 if(p==0) return; 60 shownode(p); 61 travel(ch[p][0]); 62 travel(ch[p][1]); 63 } 64 void maintain(int rt) 65 { 66 int ch0=ch[rt][0], ch1=ch[rt][1]; 67 sz[rt]=sz[ch0]+sz[ch1]+1; 68 minv[rt]=min(val[rt],min(minv[ch0],minv[ch1]));/*val*/ 69 } 70 void pushdown(int rt) 71 { 72 if(rev[rt]==0) return; 73 swap(ch[rt][0],ch[rt][1]); 74 rev[ch[rt][0]]^=1; 75 rev[ch[rt][1]]^=1; 76 rev[rt]=0; 77 } 78 void newnode(int &rt,int v) 79 { 80 rt=++tot; 81 ch[rt][0]=ch[rt][1]=0; 82 sz[rt]=1; 83 val[rt]=minv[rt]=v; 84 } 85 void build(int &rt,int *v,int l,int r) 86 { 87 if(l>r) return; 88 int m=(l+r)>>1; 89 newnode(rt,v[m]); 90 build(ch[rt][0],v,l,m-1); 91 build(ch[rt][1],v,m+1,r); 92 maintain(rt); 93 } 94 int findmin(int rt) 95 { 96 pushdown(rt);/**/ 97 if(minv[rt]==val[rt]) return sz[ch[rt][0]]+1; 98 if(minv[rt]==minv[ch[rt][0]]) return findmin(ch[rt][0]); 99 else return sz[ch[rt][0]]+1+findmin(ch[rt][1]); 100 } 101 void init(int n) 102 { 103 tot=0; 104 ch[0][0]=ch[1][1]=0; 105 sz[0]=val[0]=rev[0]=0; 106 minv[0]=n+1; 107 for(int i=1;i<=n;i++) 108 { 109 scanf("%d",&num[i]); 110 pos[i]=i; 111 } 112 sort(pos+1,pos+n+1,cmp); 113 for(int i=1;i<=n;i++) rnk[pos[i]]=i; 114 build(root,rnk,1,n); 115 } 116 void solve(int n) 117 { 118 for(int i=1;i<=n;i++) 119 { 120 //debug(); 121 int k=findmin(root); 122 printf("%d",k+i-1); 123 printf(i<n?" ":"\n"); 124 splay(root,k); 125 // debug(); 126 if(ch[root][0]) 127 { 128 rev[ch[root][0]]^=1; 129 root=merge(ch[root][0],ch[root][1]); 130 } 131 else root=ch[root][1]; 132 } 133 } 134 }spt; 135 int main() 136 { 137 //freopen("in.txt","r",stdin); 138 int n; 139 while(scanf("%d",&n),n) 140 { 141 spt.init(n); 142 spt.solve(n); 143 } 144 return 0; 145 }
View Code
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int N=100010; 6 int num[N],n; 7 bool cmp(int a,int b) 8 { 9 return num[a]<num[b] || (num[a]==num[b] && a<b); 10 } 11 struct SplayTree 12 { 13 int tot,root; 14 int ch[N][2],fa[N],sz[N]; 15 void rotate(int p,int d) 16 { 17 int f=fa[p]; 18 pushdown(f); 19 pushdown(p); 20 ch[f][d^1]=ch[p][d]; 21 if(ch[p][d]) fa[ch[p][d]]=f; 22 fa[p]=fa[f]; 23 if(fa[f]) ch[fa[f]][ch[fa[f]][1]==f]=p; 24 ch[p][d]=f; 25 fa[f]=p; 26 pushup(f); 27 } 28 void splay(int p,int g) 29 { 30 pushdown(p); 31 while(fa[p]!=g) 32 { 33 if(fa[fa[p]]==g) 34 { 35 pushdown(fa[p]); 36 pushdown(p); 37 rotate(p,ch[fa[p]][0]==p); 38 continue; 39 } 40 int f=fa[p], ff=fa[f]; 41 pushdown(ff);//先pushdown 再旋转 42 pushdown(f); 43 pushdown(p); 44 int d=(ch[ff][0]==f); 45 if(ch[f][d]==p) 46 { 47 rotate(p,d^1); 48 rotate(p,d); 49 } 50 else 51 { 52 rotate(f,d); 53 rotate(p,d); 54 } 55 } 56 pushup(p); 57 if(g==0) root=p; 58 } 59 void select(int k,int g) 60 { 61 int p=root; 62 pushdown(p); 63 while(sz[ch[p][0]]+1!=k) 64 { 65 if(k<=sz[ch[p][0]]) p=ch[p][0]; 66 else 67 { 68 k-=sz[ch[p][0]]+1; 69 p=ch[p][1]; 70 } 71 pushdown(p); 72 } 73 splay(p,g); 74 } 75 void debug(){printf("%d\n",root); travel(root);} 76 void shownode(int p) {printf("o:%d l:%d r:%d fa:%d sz:%d\n",p,ch[p][0],ch[p][1],fa[p],sz[p]);} 77 void travel(int p) 78 { 79 if(p==0) return; 80 shownode(p); 81 travel(ch[p][0]); 82 travel(ch[p][1]); 83 } 84 int rnk[N],pos[N],rev[N],pp[N]; 85 void newnode(int &p,int f) 86 { 87 p=++tot; 88 ch[p][0]=ch[p][1]=0; 89 fa[p]=f; 90 sz[p]=1; 91 rev[p]=0; 92 } 93 void pushup(int p) 94 { 95 sz[p]=sz[ch[p][0]]+sz[ch[p][1]]+1; 96 } 97 void pushdown(int p) 98 { 99 if(rev[p]==0) return; 100 rev[ch[p][0]]^=1; 101 rev[ch[p][1]]^=1; 102 swap(ch[p][0],ch[p][1]); 103 rev[p]=0; 104 } 105 void build(int &p,int l,int r,int f) 106 { 107 if(l>r) return; 108 int m=(l+r)>>1; 109 newnode(p,f); 110 pos[rnk[m]]=p; 111 build(ch[p][0],l,m-1,p); 112 build(ch[p][1],m+1,r,p); 113 pushup(p); 114 } 115 void init() 116 { 117 tot=0; 118 fa[0]=ch[0][0]=ch[0][1]; 119 sz[0]=rev[0]=0; 120 for(int i=1;i<=n;i++) 121 { 122 scanf("%d",&num[i]); 123 pp[i]=i; 124 } 125 sort(pp+1,pp+n+1,cmp); 126 for(int i=1;i<=n;i++) rnk[pp[i]]=i; 127 build(root,1,n,0); 128 } 129 void solve() 130 { 131 //debug(); 132 for(int i=1;i<=n;i++) 133 { 134 //debug(); 135 splay(pos[i],0); 136 //debug(); 137 printf("%d",i+sz[ch[root][0]]); 138 printf(i==n?"\n":" "); 139 rev[ch[root][0]]^=1; 140 if(ch[root][1]) 141 { 142 select(sz[ch[root][0]]+2,0); 143 ch[root][0]=ch[ch[root][0]][0]; 144 fa[ch[root][0]]=root; 145 pushup(root); 146 } 147 else 148 { 149 root=ch[root][0]; 150 fa[root]=0; 151 } 152 } 153 } 154 }spt; 155 156 int main() 157 { 158 //freopen("in.txt","r",stdin); 159 while(scanf("%d",&n),n) 160 { 161 spt.init(); 162 spt.solve(); 163 } 164 return 0; 165 }