// treap: split by size
#include <bits/stdc++.h>
using namespace std;

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

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

struct Node;
using PNode = Node *; // typedef Node * PNode;
struct Node
{
	int x;
	int size;
	PNode left;
	PNode right;

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

	void recalc ()
	{
		size = 1;
		if (left != nullptr)
		{
//			size += (*left).size;
			size += left -> size;
		}
		if (right != nullptr)
		{
			size += right -> size;
		}
	}
};

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

void recurOutput (PNode v)
{
	if (v == nullptr)
	{
		return;
	}
	cout << "(";
	recurOutput (v -> left);
	cout << v -> x;
	cout << "|" << v -> size;
	recurOutput (v -> right);
	cout << ")";
}

void output (PNode v)
{
	recurOutput (v);
	cout << endl;
}

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

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

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

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

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

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

int main ()
{
	int const size = 10;
	PNode root = nullptr;
	for (int i = 1; i <= size; i++)
	{
		root = tInsert (root, i);
		output (root);
	}
	for (int i = 0; i <= size; i++)
	{
		auto temp = tSplitK (root, i);
		output (temp.first);
		output (temp.second);
		root = tMerge (temp.first, temp.second);
	}
	return 0;
}
