// treap: sum on subtree, same as size
#include <cassert>
#include <iostream>
#include <random>
#include <utility>
using namespace std;

//random_device dev;
//mt19937 gen (dev ());
mt19937 gen (1234567890);

int random (int range)
{
	return uniform_int_distribution <int> (0, range - 1) (gen);
}

struct Node;
using PNode = Node *;
struct Node
{
	int x;
	int size;
	long long total;
	PNode left;
	PNode right;

	Node (int x_)
	{
		x = x_;
		size = 1;
		total = x;
		left = nullptr;
		right = nullptr;
	}

	int leftSize ()
	{
		if (left == nullptr)
		{
			return 0;
		}
		return left -> size;
	}

	void recalc ()
	{
		size = 1;
		total = x;
		if (left != nullptr)
		{
			size += left -> size;
			total += left -> total;
		}
		if (right != nullptr)
		{
			size += right -> size;
			total += right -> total;
		}
	}
};

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

void tOutputRecur (PNode t)
{
	if (t == nullptr)
	{
		return;
	}
	cout << "(";
	tOutputRecur (t -> left);
	cout << t -> x;
	tOutputRecur (t -> right);
	cout << ")";
}

void tOutput (PNode t)
{
	tOutputRecur (t);
	cout << endl;
}

pair <PNode, PNode> tSplit (PNode t, int x0)
{ // first: < x ; second: >= x
	if (t == nullptr)
	{
		return {nullptr, nullptr};
	}
	if (t -> x < x0)
	{
		auto temp = tSplit (t -> right, x0);
		t -> right = temp.first;
		t -> recalc ();
		return {t, temp.second};
	}
	else
	{
		auto temp = tSplit (t -> left, x0);
		t -> left = temp.second;
		t -> recalc ();
		return {temp.first, t};
	}
}

pair <PNode, PNode> tSplitK (PNode t, int k)
{ // first: k left; second: other
	if (t == nullptr)
	{
		return {nullptr, nullptr};
	}
//	if (k - t -> leftSize () - 1 >= 0)
	if (t -> leftSize () < k)
	{
		auto temp = tSplitK (t -> right, k - t -> leftSize () - 1);
		t -> right = temp.first;
		t -> recalc ();
		return {t, temp.second};
	}
	else
	{
		auto temp = tSplitK (t -> left, k);
		t -> left = temp.second;
		t -> recalc ();
		return {temp.first, t};
	}
}

PNode tMerge (PNode l, PNode r)
{
	if (l == nullptr)
	{
		return r;
	}
	if (r == nullptr)
	{
		return l;
	}
	if (random (l -> size + r -> size) < l -> size)
	{
		l -> right = tMerge (l -> right, r);
		l -> recalc ();
		return l;
	}
	else
	{
		r -> left = tMerge (l, r -> left);
		r -> recalc ();
		return r;
	}
}

PNode tInsert (PNode t, int x)
{
	auto temp = tSplit (t, x);
	auto v = new Node (x);
	// (temp.first | v) | temp.second
	auto half = tMerge (temp.first, v);
	return tMerge (half, temp.second);
}

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

PNode tRemove (PNode t, int x)
{
	auto one = tSplit (t, x);
	auto two = tSplit (one.second, x + 1);
	// TODO one.first | two.first | two.second
	tDelete (two.first);
	return tMerge (one.first, two.second);
}

int main ()
{
	int const size = 10;
	PNode root = nullptr;
	tOutput (root);
	for (int i = 1; i <= size * 2; i += 2)
	{
		root = tInsert (root, i);
	}
	for (int i = 0; i <= size / 2; i++)
	{
		auto one = tSplitK (root, i);
		auto two = tSplitK (one.second, size / 2);
		tOutputRecur (one.first); cout << " ";
		tOutputRecur (two.first); cout << " ";
		tOutputRecur (two.second); cout << endl;
		cout << i << ": " << two.first -> total << endl;
		one.second = tMerge (two.first, two.second);
		root = tMerge (one.first, one.second);
//		tOutput (root);
	}

	{
		auto one = tSplit (root, 11);
		auto two = tSplit (one.second, 18);
		tOutputRecur (one.first); cout << " ";
		tOutputRecur (two.first); cout << " ";
		tOutputRecur (two.second); cout << endl;
		cout << "[11..18): " << two.first -> total << endl;
		one.second = tMerge (two.first, two.second);
		root = tMerge (one.first, one.second);
	}
	return 0;
}
