Cod sursa(job #1778163)

Utilizator teoionescuIonescu Teodor teoionescu Data 13 octombrie 2016 15:53:12
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.11 kb
#include<fstream>

#define in cin
#define out cout

using namespace std;

ifstream in("algsort.in");
ofstream out("algsort.out");

class SplayTree{
private:
	struct Node{
		Node *st, *dr, *t;
		int val;
	};
	Node * root;
	void ins(Node *&n, Node *t, int x){
		if (n == NULL){
			n = new Node;
			n->st = n->dr = NULL;
			n->t = t;
			n->val = x;
			return;
		}
		if (x <= n->val) ins(n->st, n, x);
		else ins(n->dr, n, x);
	}
	void dfs(Node *n, int d){
		if (n->st) dfs(n->st, d + 1);
		//cout << "> "; for (int i = 1; i<d; i++) cout << "   "; cout << n->val << " ";
		//cout << "\n";
		cout << n->val << ' ';
		if (n->dr) dfs(n->dr, d + 1);
	}
	int isrightson(Node *T, Node *n){
		if (T->st == n) return 0;
		if (T->dr == n) return 1;
		throw("T-n problem");
	}
	void simple_swap_fathers(Node *n, Node *T){
		if (T->t != NULL){
			if (!isrightson(T->t, T)) T->t->st = n;
			else T->t->dr = n;
		}
		n->t = T->t;
		T->t = n;
	}
	void simple_right_rot(Node *n, Node *T){
		if (n->dr) n->dr->t = T;
		T->st = n->dr;
		n->dr = T;
		simple_swap_fathers(n, T);
	}
	void simple_left_rot(Node *n, Node *T){
		if (n->st) n->st->t = T;
		T->dr = n->st;
		n->st = T;
		simple_swap_fathers(n, T);
	}
	void splay(Node *n){
		root = n;
		while (1){
			Node *T = n->t;
			if (T == NULL) return;

			Node *TT = T->t;
			if (TT == NULL){
				if (!isrightson(T, n)) simple_right_rot(n, T);
				else simple_left_rot(n, T);
			}
			else{
				int a = isrightson(TT, T);
				int b = isrightson(T, n);
				if (a == 0 && b == 0){
					simple_right_rot(T, TT);
					simple_right_rot(n, T);
				}
				else if (a == 1 && b == 1){
					simple_left_rot(T, TT);
					simple_left_rot(n, T);
				}
				else if (a == 0 && b == 1){
					simple_left_rot(n, T);
					simple_right_rot(n, TT);
				}
				else if (a == 1 && b == 0){
					simple_right_rot(n, T);
					simple_left_rot(n, TT);
				}
			}
		}
	}
	Node* access(Node *n, int x){
		if (n == NULL) return NULL;
		if (n->val == x){
			return n;
		}
		if (x <= n->val) return access(n->st, x);
		else return access(n->dr, x);
	}
	void join(Node *S, Node *D){
		while (S->dr != NULL) S = S->dr;
		splay(S);
		S->dr = D;
		D->t = S;
	}
	void del(Node *x){
		splay(x);
		if (x->st == NULL && x->dr == NULL) root = NULL;
		else if (x->st == NULL){
			x->dr->t = NULL;
			root = x->dr;
		}
		else if (x->dr == NULL){
			x->st->t = NULL;
			root = x->st;
		}
		else{
			x->st->t = NULL;
			x->dr->t = NULL;
			join(x->st, x->dr);
		}
	}
public:
	void Add(int x){
		ins(root, NULL, x);
	}
	void Print(){
		if (root) dfs(root, 1);
		//cout << ">.............................................\n";
	}
	void Splay(int x){
		Node *temp = access(root, x);
		if (temp != NULL) splay(temp);
	}
	void Delete(int x){
		Node *temp = access(root, x);
		if (temp != NULL) del(temp);
	}
} T;

int main()
{
	/*for (int i = 1; i <= 9; i++){
		T.Add(i);
		T.Print();
	}
	while (1) {
		int c; int x;
		cin >> c;
		if (c == 0) return 0;
		cin >> x;
		if (c == 1) T.Splay(x);
		if (c == 2) T.Add(x);
		if (c == 3) T.Delete(x);
		cout << "\n";
		T.Print();
	}*/
	int n; cin >> n;
	for (int i = 1; i <= n; i++){
		int x;
		cin >> x;
		T.Add(x);
		T.Splay(x);
	}
	T.Print(); cout<<"\n";
	return 0;
}