Advertisement
Guest User

Untitled

a guest
Apr 13th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<assert.h>
  4. #include<string.h>
  5. #include<time.h>
  6. #include<stdlib.h>
  7. #include<math.h>
  8. #include<string>
  9. #include<sstream>
  10. #include<map>
  11. #include<set>
  12. #include<queue>
  13. #include<stack>
  14. #include<vector>
  15. #include<algorithm>
  16. #pragma comment(linker, "/STACK:16777216")
  17. #define pb push_back
  18. #define ppb pop_back
  19. #define mp make_pair
  20. #define all(x) (x).begin(),(x).end()
  21. #define sz(x) (int)(x).size()
  22. #define LL long long
  23. #define bit __builtin_popcountll
  24. using namespace std;
  25. template<class T> inline T sqr(T x) { return x * x; }
  26. typedef pair<int, int> pii;
  27. const double eps = 1e-9;
  28. const double pi = acos(-1.0);
  29. const int maxn = (int)1e5 + 10;
  30. struct node
  31. {
  32. int y;
  33. int cnt;
  34. bool rev;
  35. node *p;
  36. node *l;
  37. node *r;
  38. node() : y(rand() % 1000000000), cnt(1), rev(false), p(NULL), l(NULL), r(NULL) {}
  39. };
  40. struct belt
  41. {
  42. int x;
  43. int id;
  44. };
  45. typedef node *pnode;
  46. int cnt(pnode T)
  47. {
  48. return T ? T -> cnt : 0;
  49. }
  50. void upd_cnt(pnode T)
  51. {
  52. if (T) T -> cnt = cnt(T -> l) + cnt(T -> r) + 1;
  53. }
  54. void push(pnode T)
  55. {
  56. if (T && T -> rev)
  57. {
  58. T -> rev = false;
  59. swap(T -> l,T -> r);
  60. if (T -> l) T -> l -> rev ^= true;
  61. if (T -> r) T -> r -> rev ^= true;
  62. }
  63. }
  64. void split(pnode T, pnode &L, pnode &R, int key, int add = 0)
  65. {
  66. push(T);
  67. if (!T) return void(L = R = NULL);
  68. int cur_key = cnt(T -> l) + add;
  69. if (cur_key >= key)
  70. {
  71. split(T -> l,L,T -> l,key,add);
  72. if (T -> l) T -> l -> p = T;
  73. if (L) L -> p = NULL;
  74. R = T;
  75. } else
  76. {
  77. split(T -> r,T -> r,R,key,add + cnt(T -> l) + 1);
  78. if (T -> r) T -> r -> p = T;
  79. if (R) R -> p = NULL;
  80. L = T;
  81. }
  82. upd_cnt(T);
  83. }
  84. void merge(pnode &T, pnode L, pnode R)
  85. {
  86. push(L);
  87. push(R);
  88. if (!L || !R) T = L ? L : R; else
  89. if (L -> y > R -> y)
  90. {
  91. merge(L -> r,L -> r,R);
  92. if (L -> r) L -> r -> p = L;
  93. T = L;
  94. } else
  95. {
  96. merge(R -> l,L,R -> l);
  97. if (R -> l) R -> l -> p = R;
  98. T = R;
  99. }
  100. upd_cnt(T);
  101. }
  102. pnode order[maxn],T,st[maxn];
  103. belt A[maxn];
  104. int p[maxn],pp[maxn];
  105. bool cmp(int i, int j)
  106. {
  107. return A[i].x < A[j].x || A[i].x == A[j].x && A[i].id < A[j].id;
  108. }
  109. void insert(int x)
  110. {
  111. pnode P = new node();
  112. order[pp[x]] = P;
  113. merge(T,T,P);
  114. }
  115. int pos(pnode T)
  116. {
  117. int c = 0;
  118. while(T)
  119. {
  120. st[c++] = T;
  121. T = T -> p;
  122. }
  123. int add = 0;
  124. for(int i = c - 1; i > 0; i--)
  125. {
  126. push(st[i]);
  127. if (st[i] -> l == st[i - 1])
  128. {
  129. continue;
  130. } else
  131. {
  132. add += 1 + cnt(st[i] -> l);
  133. }
  134. }
  135. push(st[0]);
  136. add += cnt(st[0] -> l);
  137. return add + 1;
  138. }
  139. void swapper(int l, int r)
  140. {
  141. pnode L,MID,R;
  142. split(T,L,R,l);
  143. split(R,MID,R,r - l + 1);
  144. if (MID) MID -> rev ^= true;
  145. merge(T,L,MID);
  146. merge(T,T,R);
  147. split(T,L,T,1);
  148. }
  149. int main()
  150. {
  151. #ifndef ONLINE_JUDGE
  152. freopen("input.txt","r",stdin);
  153. freopen("output.txt","w",stdout);
  154. #endif
  155. srand(time(0));
  156. int n;
  157. while(true)
  158. {
  159. scanf("%d",&n);
  160. if (n == 0) break;
  161. T = NULL;
  162. for(int i = 0; i < n; i++)
  163. {
  164. int x;
  165. scanf("%d",&x);
  166. A[i].x = x;
  167. A[i].id = i;
  168. p[i] = i;
  169. }
  170. sort(p,p + n,cmp);
  171. for(int i = 0; i < n; i++)
  172. pp[p[i]] = i;
  173. for(int i = 0; i < n; i++)
  174. insert(i);
  175. for(int i = 0; i < n; i++)
  176. {
  177. int position = pos(order[i]);
  178. printf("%d ",position + i);
  179. swapper(0,position - 1);
  180. }
  181. puts("");
  182. }
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement