// set: insert, erase, find
// + kth order statistic
#include <cstdlib>
#include <iostream>
using namespace std;

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

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

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

	void recalc ();
};

int getSize (PNode x)
{
	return (x == nullptr) ? 0 : x -> size;
}

void Node::recalc ()
{
	size = 1 + getSize (left) + getSize (right);
}

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;
		t -> recalc ();
		return {temp.first, t};
	}
	else
	{
		auto temp = tsplit (t -> right, xs);
		t -> right = temp.first;
		t -> recalc ();
		return {t, temp.second};
	}
}

pair <PNode, PNode> tsplitK (PNode t, int k)
{
	if (t == nullptr) return {nullptr, nullptr};
	int leftSize = getSize (t -> left);
//	if (leftSize >= k)
	if (k - leftSize - 1 < 0)
	{
		auto temp = tsplitK (t -> left, k);
		// temp.first | {temp.second | t}
		t -> left = temp.second;
		t -> recalc ();
		return {temp.first, t};
	}
	else
	{
		auto temp = tsplitK (t -> right, k - leftSize - 1);
		t -> right = temp.first;
		t -> recalc ();
		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);
		r -> recalc ();
		return r;
	}
	else
	{
		l -> right = tmerge (l -> right, r);
		l -> recalc ();
		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);
}

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;
}

int main ()
{
	int n = 10;
	PNode root = nullptr;
	for (int i = 0; i < n; i++)
	{
		root = tinsert (root, (i * 3) % n);
		toutput (root);
	}

	{
		auto temp = tsplitK (root, 6);
		toutput (temp.first);
		toutput (temp.second);
		root = tmerge (temp.first, temp.second);
		toutput (root);
	}

	{
		auto temp = tsplitK (root, 6);
		auto right = tsplitK (temp.second, 1);
		cout << right.first -> x << endl;
		temp.second = tmerge (right.first, right.second);
		root = tmerge (temp.first, temp.second);
	}

	for (int i = 0; i < 2 * n; i++)
	{
		cout << tfind (root, i);
	}
	cout << endl;

	for (int i = 0; i < n; i++)
	{
		root = terase (root, (i * 7) % n);
		toutput (root);
	}

	return 0;
}
