Cod sursa(job #1852515)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 20 ianuarie 2017 21:09:17
Problema Sortare prin comparare Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 2.87 kb
//#define fastcall __attribute__((optimize("-O3")))
//#define inline __inline__ __attribute__((always_inline))

#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;

const int NMAX = 5e5 + 5,
          BMAX = 1 << 15,
           nil = 0;

struct PII {
    int x, y; };

struct T {
    unsigned short int pr;
    int key, st, dr;

    inline T() {
        key = -1;
        st = nil;
        dr = nil;
        pr = rand() & ((short int) 0xffff); }

    inline T(int _key) {
        key = _key;
        pr = rand() & ((short int) 0xffff);
        st = nil;
        dr = nil; } };

int up;

char ibuff[BMAX], ubuff[BMAX];
T tb[NMAX];

inline char nextch(void) {
    static int bp = BMAX;

    if (bp == BMAX) {
        bp = 0;
        fread(ibuff, 1, BMAX, stdin); }

    return ibuff[ bp++ ]; }

inline void get(int &arg) {
    char ch;

    do {
        ch = nextch(); }
    while (ch < '0' || ch > '9');
    arg = 0;
    do {
        arg = arg * 10 + ch - '0';
        ch = nextch(); }
    while ('0' <= ch && ch <= '9'); }

inline void putch(const char &ch) {
    ubuff[up++] = ch;
    if (up == BMAX) {
        fwrite(ubuff, 1, BMAX, stdout);
        up = 0; } }

inline void put(int n) {
    static char digs[10];
    int d = 0, q;

    do {
        q = n / 10;
        digs[d++] = n - (q << 1) - (q << 3) + '0';
        n = q; }
    while (n);
    while (d--)
        putch(digs[d]); }

inline int newT(const int &val) {
    static int bp = 0;
    tb[++bp] = T(val);
    return bp; }

int join(const int &a, const int &b) {
    if (!a) return b;
    if (!b) return a;

    if (tb[a].pr >= tb[b].pr) {
        tb[a].dr = join(tb[a].dr, b);
        return a; }
    else {
        tb[b].st = join(a, tb[b].st);
        return b; } }

int join3(const int &a, const int &b, const int &c) {
    return join(a, join(b, c)); }

PII split(const int &nod, const int &val) {
    if (nod == 0) return {0, 0};

    if (tb[nod].key <= val) {
        PII tmp = split(tb[nod].dr, val);
        tb[nod].dr = tmp.x;
        return {nod, tmp.y}; }
    else {
        PII tmp = split(tb[nod].st, val);
        tb[nod].st = tmp.y;
        return {tmp.x, nod}; } }

void dfs(const int &nod) {
    if (tb[nod].st) dfs(tb[nod].st);
    put(tb[nod].key);
    putch(' ');
    if (tb[nod].dr) dfs(tb[nod].dr); }

inline int insert(const int &nod, const int &val) {
    PII tmp = split(nod, val);
    return join3(tmp.x, newT(val), tmp.y); }

int main(void) {
    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);
    int root, n, t;

    srand(time(NULL));
    tb[0].st = 0;
    tb[0].dr = 0;
    tb[0].pr = -1;
    root = nil;

    get(n);
    for (int i = 0; i < n; ++i) {
        get(t);
        root = insert(root, t); }

    dfs(root);
    putch('\n');
    fwrite(ubuff, 1, up, stdout);

    fprintf(stderr, "%d\n", sizeof(tb) + sizeof(ibuff) + sizeof(ubuff) + sizeof(*stdin) + sizeof(*stdout));

    return 0; }