Robotic Sort

Posted by Songtoy on 2017-09-04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long
#define keyTree (son[son[rt][1]][0])
static const int INF = 2000000000;
static const int M = 100005;
int obj[M],A[M];
struct node{
int x,pos;
bool operator <(const node &tmp)const{
if(x!=tmp.x)return x<tmp.x;
return pos<tmp.pos;
}
}Q[M];
struct SplayTree{
int fa[M],son[M][2];
int val[M],sz[M],mi[M];
bool lazy[M];
int rt,tot;
void clear(){
rt=tot=0;
memset(lazy,false,sizeof(lazy));
memset(fa,0,sizeof(fa));
memset(son,0,sizeof(son));
memset(val,0,sizeof(val));
memset(sz,0,sizeof(sz));
}
SplayTree(){
clear();
}
void Newnode(int &u,int f,int x){
u=++tot;
fa[u]=f;
mi[u]=val[u]=x;
lazy[u]=false;
sz[u]=1;
son[u][0]=son[u][1]=0;
if(!rt)rt=u;
}
void Up(int p){
sz[p]=1;
lazy[p]=false;
mi[p]=val[p];
if(son[p][0]){
sz[p]+=sz[son[p][0]];
mi[p]=min(mi[p],mi[son[p][0]]);
}
if(son[p][1]){
sz[p]+=sz[son[p][1]];
mi[p]=min(mi[p],mi[son[p][1]]);
}
}
void Rev(int p){
lazy[p]^=true;
int t=son[p][0];
son[p][0]=son[p][1];
son[p][1]=t;
}
void Down(int p){
if(lazy[p]){
if(son[p][0])Rev(son[p][0]);
if(son[p][1])Rev(son[p][1]);
lazy[p]=false;
}
}
void Rotate(int p){
int F=fa[p];
int d=son[F][1]==p;
son[F][d]=son[p][!d];
if(son[p][!d])fa[son[p][!d]]=F;
fa[p]=fa[F];
son[fa[p]][son[fa[p]][1]==F]=p;
fa[F]=p;
son[p][!d]=F;
//
Up(F);
}
void Splay(int p,int goal){
while(fa[p]!=goal){
int F=fa[p];
if(fa[F]!=goal){
if(son[F][1]==p^son[fa[p]][1]==F)Rotate(p);
else Rotate(F);
}
Rotate(p);
}
Up(p);
if(!goal)rt=p;
}
void RotateTo(int k,int goal){//(k+1)大
int p=rt;
Down(p);
while(sz[son[p][0]]!=k){
if(sz[son[p][0]]>k)p=son[p][0];
else{
k-=sz[son[p][0]]+1;
p=son[p][1];
}
Down(p);
}
Splay(p,goal);
}
void Insert(int n){
for(int i=1;i<=n;++i){
scanf("%d",&A[i]);
Q[i]=(node){A[i],i};
}
sort(Q+1,Q+1+n);
for(int i=1;i<=n;++i){
A[Q[i].pos]=i;
}
Newnode(rt,0,INF);
Newnode(son[rt][1],rt,INF);
build(keyTree,son[rt][1],1,n);
Up(son[rt][1]);
Up(rt);
}
void build(int &u,int f,int L,int R){
int mid=L+R>>1;
Newnode(u,f,A[mid]);
obj[A[mid]]=u;
if(L==R)return ;
if(L!=mid)build(son[u][0],u,L,mid-1);
build(son[u][1],u,mid+1,R);
Up(u);
}
void Solve(int pos,int n){
int L=pos;
RotateTo(L-1,0);
RotateTo(n+1,rt);
int p=keyTree,R=0;
Down(p);
while(val[p]!=mi[p]){
if(mi[p]==mi[son[p][0]]){
p=son[p][0];
}else{
R+=sz[son[p][0]]+1;
p=son[p][1];
}
Down(p);
}
R+=sz[son[p][0]];//易错点
//
R+=L;
printf("%d",R);
if(pos!=n)putchar(' ');
RotateTo(R+1,rt);
Rev(keyTree);
}
void debug(int p){
printf("v: %d , sz: %d , lazy: %d , val: %d , mi: %d\n",p,sz[p],lazy[p],val[p],mi[p]);
printf("%d %d\n",son[p][0],son[p][1]);
if(son[p][0])debug(son[p][0]);
// printf("%d ",val[p]);
if(son[p][1])debug(son[p][1]);
}
}Tree;
int main(){
int n;
while(~scanf("%d",&n),n){
Tree.clear();
Tree.Insert(n);
for(int i=1;i<=n;++i)Tree.Solve(i,n);
puts("");
}
return 0;
}

$By SongToy$