Advertisement
Guest User

Spoj Robotic Sort

a guest
Apr 13th, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7. const int MAXN = 100000;
  8.  
  9.  
  10. typedef struct item * tree;
  11. struct item {
  12.     tree l, r, p;
  13.     int prior, cnt;
  14.     bool rev;
  15.  
  16.     void init() {
  17.         cnt = 1;
  18.         rev = false;
  19.         l = r = p = 0;
  20.     }
  21. };
  22.  
  23. inline int cnt (tree t) {
  24.     return t ? t->cnt : 0;
  25. }
  26.  
  27. inline void push (tree t) {
  28.     if (!t)  return;
  29.     if (t->l)  t->l->p = t;
  30.     if (t->r)  t->r->p = t;
  31.     if (t->rev) {
  32.         t->rev = false;
  33.         swap (t->l, t->r);
  34.         if (t->l)  t->l->rev ^= true;
  35.         if (t->r)  t->r->rev ^= true;
  36.     }
  37. }
  38.  
  39. inline void upd_cnt (tree t) {
  40.     if (t) {
  41.         t->cnt = cnt (t->l) + cnt (t->r) + 1;
  42.         push (t);
  43.     }
  44. }
  45.  
  46. tree merge (tree l, tree r) {
  47.     push (l),  push (r);
  48.     if (!l || !r)
  49.         return l ? l : r;
  50.     if (l->prior > r->prior) {
  51.         l->r = merge (l->r, r);
  52.         upd_cnt (l);
  53.         return l;
  54.     }
  55.     else {
  56.         r->l = merge (l, r->l);
  57.         upd_cnt (r);
  58.         return r;
  59.     }
  60. }
  61.  
  62. void split (tree t, tree & l, tree & r, int key) {
  63.     if (!t) {
  64.         l = r = 0;
  65.         return;
  66.     }
  67.     push (t);
  68.     int cur_key = cnt (t->l) + 1;
  69.     if (key < cur_key) {
  70.         split (t->l, l, t->l, key);
  71.         r = t;
  72.     }
  73.     else {
  74.         split (t->r, t->r, r, key - cur_key);
  75.         l = t;
  76.     }
  77.     upd_cnt (l),  upd_cnt (r);
  78.     if (l)  l->p = 0;
  79.     if (r)  r->p = 0;
  80. }
  81.  
  82. void recpush (tree t) {
  83.     if (t->p)  recpush (t->p);
  84.     push (t);
  85. }
  86.  
  87. int up (tree t) {
  88.     recpush (t);
  89.     int res = cnt (t->l);
  90.     while (t->p) {
  91.         if (t->p->r == t)
  92.             res += cnt (t->p->l) + 1;
  93.         t = t->p;
  94.     }
  95.     return res;
  96. }
  97.  
  98.  
  99. int n;
  100. pair<int,int> a[MAXN];
  101. int p[MAXN];
  102. item ver[MAXN];
  103. char buf[MAXN*10], *ptr;
  104.  
  105.  
  106. inline int read_int() {
  107.     while (*ptr == ' ')  ++ptr;
  108.     int res = 0;
  109.     while ('0' <= *ptr && *ptr <= '9')
  110.         res = res * 10 + *ptr++ - '0';
  111.     return res;
  112. }
  113.  
  114. inline void print_int (int n) {
  115.     char * beg = ptr;
  116.     while (n) {
  117.         *ptr++ = char ('0' + n % 10);
  118.         n /= 10;
  119.     }
  120.     reverse (beg, ptr);
  121. }
  122.  
  123.  
  124. bool read() {
  125.     gets (buf);
  126.     ptr = buf;
  127.     n = read_int();
  128.     if (n == 0)
  129.         return false;
  130.  
  131.     gets (buf);
  132.     ptr = buf;
  133.     for (int i=0; i<n; ++i)
  134.         a[i] = make_pair (read_int(), i);
  135.     return true;
  136. }
  137.  
  138.  
  139. void solve() {
  140.     sort (a, a+n);
  141.     for (int i=0; i<n; ++i) {
  142.         p[a[i].second] = i;
  143.         ver[i].init();
  144.     }
  145.  
  146.     tree t = 0;
  147.     for (int i=0; i<n; ++i)
  148.         t = merge (t, &ver[p[i]]);
  149.  
  150.     ptr = buf;
  151.     for (int i=0; i<n; ++i) {
  152.         int key = up (&ver[i]);
  153.  
  154.         tree t1, t2, t3;
  155.         split (t, t1, t2, key);
  156.         split (t2, t2, t3, 1);
  157.         if (t1)
  158.             t1->rev ^= true;
  159.         t = merge (t1, t3);
  160.  
  161.         if (i)
  162.             *ptr++ = ' ';
  163.         print_int (key + i + 1);
  164.     }
  165.     *ptr = 0;
  166.     puts (buf);
  167. }
  168.  
  169.  
  170. int main() {
  171.  
  172.     for (int i=0; i<MAXN; ++i)
  173.         ver[i].prior = rand();
  174.  
  175.     while (read())
  176.         solve();
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement