Hdu 1089 Robotic Sort

Source: Internet
Author: User

Question: splay, interval reversal.

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <vector>
# Include <stack>
# Include <list>
# Include <iostream>
# Include <map>
Using namespace std;
# Define inf 0x3f3f3f
# Deprecision Max 110
# Define M 101000
# Define keytree ch [ch [root] [1] [0]
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
Struct node
{
Int v, id;
Bool operator <(const node a) const
{
If (v = a. v)
Return id <a. id;
Return v <a. v;
}
} A [101000];
Int B [101000], n;
Int p [M], ch [M] [2], v [M], s [M], mi [M], rev [M];
Int top1, root;
Void visit (int x)
{
If (! X) return;
Printf ("x % d p % d ch0 % d ratio % d v % d s % d mi % d co % d \ n", x, p [x], ch [x] [0], ch [x] [1], v [x], s [x], mi [x], rev [x]);
Visit (ch [x] [0]);
Visit (ch [x] [1]);
}
Void debug ()
{
Printf ("debug \ n ");
Visit (root );
}
Void newnode (int & x, int val, int pre)
{
X = ++ top1;
Ch [x] [0] = ch [x] [1] = rev [x] = 0;
S [x] = 1;
Mi [x] = v [x] = val;
P [x] = pre;
}
Void up (int x)
{
Int l = ch [x] [0], r = ch [x] [1];
S [x] = 1 + s [l] + s [r];
Mi [x] = min (mi [l], mi [r]);
Mi [x] = min (mi [x], v [x]);
}
Void down (int x)
{
If (! Rev [x]) return;
Int l = ch [x] [0], r = ch [x] [1];
If (l)
{
Swap (ch [l] [0], ch [l] [1]);
Rev [l] ^ = 1;
}
If (r)
{
Swap (ch [r] [0], ch [r] [1]);
Rev [r] ^ = 1;
}
Rev [x] = 0;
}
Void build (int & x, int l, int r, int pre)
{
If (l> r) return;
Int mid = (l + r)> 1;
Newnode (x, B [mid], pre );
Build (ch [x] [0], l, mid-1, x );
Build (ch [x] [1], mid + 1, r, x );
Up (x );
}
Void init ()
{
Top1 = 0;
V [0] = mi [0] = inf;
Newnode (root,-inf, 0 );
Newnode (ch [root] [1], inf, root );
S [root] = 2;
Build (keytree, 1, n, ch [root] [1]);
Up (ch [root] [1]);
Up (root );
}
Void rot (int x, int f)
{
Int y = p [x];
Down (x), down (y );
P [ch [x] [f] = y;
Ch [y] [! F] = ch [x] [f];
P [x] = p [y];
If (p [y]) ch [p [y] [ch [p [y] [1] = y] = x;
P [y] = x;
Ch [x] [f] = y;
Up (y );
}
Void splay (int x, int goal)
{
While (p [x]! = Goal)
{
If (p [p [x] = goal) rot (x, ch [p [x] [0] = x );
Else
{
Int y = p [x], f = ch [p [y] [0] = y;
If (ch [y] [f] = x)
Rot (x ,! F );
Else
Rot (y, f );
Rot (x, f );
}
}
Up (x );
If (! Goal) root = x;
}
Void roal (int k, int goal)
{
Int x = root;
Down (x );
While (s [ch [x] [0]! = K)
{
If (s [ch [x] [0]> k)
X = ch [x] [0];
Else
{
K-= s [ch [x] [0] + 1;
X = ch [x] [1];
}
Down (x );
}
Splay (x, goal );
}
Void getmin (int l, int r)
{
Rotto (L-1, 0 );
Rotto (r + 1, root );
Int x = keytree;
Int num = 1;
Down (x );
While (v [x]! = L)
{
If (mi [ch [x] [0] = l)
X = ch [x] [0];
Else
{
Num + = s [ch [x] [0] + 1;
X = ch [x] [1];
}
Down (x );
}
Num + = s [ch [x] [0];
Rotto (l + num, root );
Rev [keytree] = 1;
Swap (ch [keytree] [0], ch [keytree] [1]);
If (l = 1)
Printf ("% d", l + num-1 );
Else
Printf ("% d", l + num-1 );
}
Int main ()
{
Int I;
While (scanf ("% d", & n), n)
{
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & a [I]. v );
A [I]. id = I;
}
Sort (a + 1, a + n + 1 );
For (I = 1; I <= n; I ++)
{
B [a [I]. id] = I;
}
Init ();
For (I = 1; I <= n; I ++)
Getmin (I, n );
Puts ("");
}
}

Author: Wings_of_Liberty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.