Pagini recente » Cod sursa (job #334619) | Monitorul de evaluare | Cod sursa (job #2672298) | Cod sursa (job #1267769) | Cod sursa (job #1228918)
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
#define N 500001
#define dim 8192
char ax[dim];
int pz;
inline void cit(int &x) {
x = 0;
while (ax[pz] < '0' || ax[pz] > '9')
if (++pz == dim)
fread(ax, 1, dim, stdin), pz = 0;
while (ax[pz] >= '0' && ax[pz] <= '9') {
x = x * 10 + ax[pz] - '0';
if (++pz == dim)
fread (ax, 1, dim, stdin), pz = 0;
}
}
struct Node {
int v, p;
Node *left, *right;
};
Node *root, *nil;
void init(Node *&root) {
nil = new Node;
nil->left = nil->right = 0;
nil->v = nil->p = -0x3f3f3f3f;
root = nil;
}
inline void rotleft(Node *&n) {
Node *t = n->left;
n->left = t->right;
t->right = n;
n = t;
}
inline void rotright(Node *&n) {
Node *t = n->right;
n->right = t->left;
t->left = n;
n = t;
}
inline void insert(Node *&n, int v) {
if (n == nil) {
n = new Node;
n->left = n->right = nil;
n->v = v;
n->p = rand();
return;
}
if (v <= n->v) {
insert(n->left, v);
if (n->left->p > n->p)
rotleft(n);
}
if (v > n->v) {
insert(n->right, v);
if (n->right->p > n->p)
rotright(n);
}
}
void afis(Node *n) {
if (n == nil)
return;
afis(n->left);
printf ("%d ", n->v);
afis(n->right);
}
int main() {
srand(time(0));
freopen ("algsort.in", "r", stdin);
freopen ("algsort.out", "w", stdout);
int n, v;
cit(n);
init(root);
int i;
for (i = 1; i <= n; ++i) {
cit(v);
insert(root, v);
}
afis(root);
}