#include <cstdlib>
#include <iostream>
using namespace std;

int random ()
{
	return (rand () << 15) ^ rand ();
}

struct Node;
typedef Node * PNode;
struct Node
{
	int x;
	int y;
	PNode left;
	PNode right;

	Node (int x_)
	{
		x = x_;
		y = random ();
		left = nullptr;
		right = nullptr;
	}
};

bool tfind (PNode t, int xf)
{
	if (t == nullptr) return false;
	if (t -> x == xf) return true;
	if (t -> x < xf) return tfind (t -> right, xf);
	else return tfind (t -> left, xf);
}

pair <PNode, PNode> tsplit (PNode t, int xs)
{
	if (t == nullptr) return {nullptr, nullptr};
	if (xs < t -> x)
	{
		auto temp = tsplit (t -> left, xs);
		// temp.first | {temp.second | t}
		t -> left = temp.second;
		return {temp.first, t};
	}
	else
	{
		auto temp = tsplit (t -> right, xs);
		t -> right = temp.first;
		return {t, temp.second};
	}
}

PNode tmerge (PNode l, PNode r)
{ // all x in l <= all x in r
	if (l == nullptr) return r;
	if (r == nullptr) return l;
	if (l -> y < r -> y)
	{
		r -> left = tmerge (l, r -> left);
		return r;
	}
	else
	{
		l -> right = tmerge (l -> right, r);
		return l;
	}
}

PNode tinsert (PNode t, int x)
{
	auto temp = tsplit (t, x);
	auto v = new Node (x);
	// x: temp.first <= v < temp.second
	auto half = tmerge (v, temp.second);
	return tmerge (temp.first, half);
}

void tdelete (PNode t)
{
	if (t == nullptr) return;
	tdelete (t -> left);
	tdelete (t -> right);
	delete t;
}

PNode terase (PNode t, int x)
{
	auto temp = tsplit (t, x);
	auto half = tsplit (temp.second, x + 1);
	// <x: temp.first    =x: half.first    >x: half.second
	tdelete (half.first);
	return tmerge (temp.first, half.second);
}

int main ()
{
	int n = 10;
	PNode root = new Node (12345);
	cout << root -> x << " ";
	cout << root -> y << "\n";
	cout << (*root).x << " ";
	cout << (*root).y << "\n";
	delete root;
	return 0;
}
