Cod sursa(job #1936567)

Utilizator ArkinyStoica Alex Arkiny Data 23 martie 2017 10:57:06
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.64 kb
#include <fstream>
#include<algorithm>
#include<vector>
#include<queue>
#include<algorithm>
#include<time.h>
using namespace std;

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

class Implicit_Treap
{
public:
	struct Node
	{
		Node *l, *r;

		int priority;
		bool lz;
		int value;
		int sz;
		Node(int data)
		{
			this->priority = rand()+1;

			l = r = 0;
			lz = 0;
			value = data;
			sz = 1;
		}

	}*root;
private:
	void push(Node *node)
	{
		if (node)
		{
			if (node->lz)
				swap(node->l, node->r);
			if (node->r)
				node->r->lz ^= node->lz;
			if (node->l)
				node->l->lz ^= node->lz;

			node->lz = 0;
		}
	}

	void update(Node *node)
	{
		if (node)
		{
			node->sz = 1;
			if (node->l)
				node->sz += node->l->sz;

			if (node->r)
				node->sz += node->r->sz;
		}
	}

	void split(Node *root, Node *&tr1, Node *&tr2, int k)
	{
		if (root)
		{
			push(root);
			int sz_l = root->l ? root->l->sz : 0;
			if (sz_l + 1 <= k)
			{
				split(root->r, root->r, tr2, k-sz_l-1);

				tr1 = root;

				update(tr1);
			}
			else
			{
				split(root->l, tr1, root->l, k);

				tr2 = root;

				update(tr2);
			}
		}
		else
		{
			tr1 = NULL;
			tr2 = NULL;
		}

	}

	void merge(Node *&root, Node *tr1, Node*tr2)
	{
		if (!tr1 || !tr2)
		{

			if (tr1)
			{
				push(tr1);
				update(tr1);

				root = tr1;
			}
			if (tr2)
			{
				push(tr2);
				update(tr2);

				root = tr2;
			}

			return;
		}

		push(tr1);
		push(tr2);
		
		if (tr1->priority > tr2->priority)
		{
			merge(tr1->r, tr1->r, tr2);

			root = tr1;

		}
		else
		{
			merge(tr2->l, tr1, tr2->l);

			root = tr2;
		}

		update(root);

	}

	

	void print(Node *root)
	{
		if (root)
		{
			push(root);

			print(root->l);

			out << root->value << " ";

			print(root->r);
		}
	}

	void clear(Node *&root)
	{
		if (root)
		{
			clear(root->l);

			clear(root->r);

			delete root;

			root = NULL;

		}
	}

public:
	Implicit_Treap()
	{
		root = NULL;
	}

	void merge(Implicit_Treap *tr1, Implicit_Treap *tr2)
	{
		merge(root, tr1->root, tr2->root);
	}

	void split(Implicit_Treap *tr1, Implicit_Treap *tr2, int key)
	{
		split(root, tr1->root, tr1->root, key);
	}

	void insert(int position, int value)
	{
		Node *p = new Node(value);

		Node *l, *r;

		split(root, l, r, position - 1);

		Node *x;

		merge(x, l, p);

		merge(root, x, r);

	}

	void remove(int x, int y)
	{
		Node *l, *r, *l1, *r1;

		split(root, l, r, y);

		split(l, l1, r1, x - 1);

		clear(r1);

		merge(root, l1, r);
	}

	int query(int x)
	{
		Node *l, *r, *l1, *r1;

		split(root, l, r, x);

		split(l, l1, r1, x - 1);

		int rez = r1->value;

		merge(l, l1, r1);

		merge(root, l, r);

		return rez;

	}

	void reverse(int x, int y)
	{
		Node *l, *r, *l1, *r1;

		split(root, l, r, y);

		split(l, l1, r1, x - 1);

		r1->lz ^= 1;

		push(r1);

		merge(l, l1, r1);

		merge(root, l, r);
	}

	void clear()
	{
		clear(root);
	}

	void print()
	{
		print(root);
	}

};

int main() {
   

	srand(time(NULL));


	int N, M;

	in >> N >> M;

	Implicit_Treap t;


	for (int i = 1; i <= N; ++i)
	{
		char op;

		in >> op;

		if (op == 'I')
		{
			int x, y;

			in >> x >> y;

			t.insert(x, y);
		}
		else if (op == 'A')
		{
			int x;

			in >> x;

			out << t.query(x) << "\n";
		}
		else if (op == 'R')
		{
			int x, y;

			in >> x >> y;

			t.reverse(x, y);
		}
		else if (op == 'D')
		{
			int x, y;

			in >> x >> y;

			t.remove(x, y);
		}

	}

	t.print();
  
	return 0;
}