Cod sursa(job #1961489)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 11 aprilie 2017 10:10:11
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.75 kb
#include <bits/stdc++.h>
using namespace std;

FILE *in, *out;
char buffer[1 << 17];
int p;
void next_char();
int get_int();
char get_char();

typedef struct Treap * Arbore;
typedef pair <Arbore, Arbore> Paa;
Arbore NIL;
struct Treap
{
	int val, prio = rand(), g = 1;
	bool lazy;
	Arbore st = NIL, dr = NIL;
	Treap(int _val = 0) : val(_val) { }
	~Treap() { if (st != NIL) { delete st; } if (dr != NIL) { delete dr; } }
};

void reset(Arbore k);
void propagate(Arbore k);
Arbore insert(Arbore k, int val, int poz);
Arbore erase(Arbore k, int st, int dr);
Arbore rotate(Arbore k, int st, int dr);
Arbore join(Arbore a, Arbore b);
Paa split(Arbore k, int poz);
void inorder(Arbore k);
int access(Arbore k, int poz);

int main()
{
	srand(time(0));
	in = fopen("secv8.in", "r");
	out = fopen("secv8.out", "w");
	//next_char();
	NIL = new Treap();
	NIL->st = NIL->dr = NIL;
	NIL->g = 0;
	Arbore k = NIL;

	int n, q, a, b;//(get_int()), q(get_int()), a, b;
	fscanf(in, "%d%d", &n, &q);
	char op;

	while (n--) {
		op = 0;
		while (!isalnum(op))
			fscanf(in, "%c", &op);
		//op = get_char();
		if (op == 'I') {
			fscanf(in, "%d%d", &a, &b);
			//a = get_int(), b = get_int();
			k = insert(k, b, a);
		}
		else if (op == 'A') {
			fscanf(in, "%d", &a);
			//a = get_int();
			fprintf(out, "%d\n", access(k, a));
		}
		else if (op == 'R') {
			fscanf(in, "%d%d", &a, &b);
			//a = get_int(), b = get_int();
			k = rotate(k, a, b);
		}
		else {
			fscanf(in, "%d%d", &a, &b);
			//a = get_int(), b = get_int();
			k = erase(k, a, b);
		}
	}
	inorder(k);
	return 0;
}


void inorder(Arbore k)
{
	propagate(k);
	if (k == NIL)
		return;
	inorder(k->st);
	fprintf(out, "%d ", k->val);
	inorder(k->dr);
}

int access(Arbore k, int poz)
{
	propagate(k);
	if (k == NIL)
		return -1;
	if (poz == k->st->g + 1)
		return k->val;
	if (poz <= k->st->g)
		return access(k->st, poz);
	return access(k->dr, poz - 1 - k->st->g);
}

void reset(Arbore k)
{
	k->g = 1 + k->st->g + k->dr->g;
}

void propagate(Arbore k)
{
	if (k == NIL)
		return;
	if (k->lazy) {
		swap(k->st, k->dr);
		k->st->lazy ^= 1;
		k->dr->lazy ^= 1;
		k->lazy = 0;
	}
}

Arbore insert(Arbore k, int val, int poz)
{
	Arbore x = new Treap(val);
	Paa s(split(k, poz));
	return join(s.first, join(x, s.second));
}

Arbore erase(Arbore k, int st, int dr)
{
	Paa x(split(k, dr + 1));
	Paa y(split(x.first, st));
	if (y.second != NIL)
		delete y.second;
	return join(y.first, x.second);
}

Arbore rotate(Arbore k, int st, int dr)
{
	Paa x(split(k, dr + 1));
	Paa y(split(x.first, st));
	y.second->lazy ^= 1;
	return join(y.first, join(y.second, x.second));
}

Arbore join(Arbore a, Arbore b)
{
	if (a == NIL)
		return b;
	if (b == NIL)
		return a;
	propagate(a);
	propagate(b);

	if (a->prio >= b->prio) {
		a->dr = join(a->dr, b);
		reset(a);
		return a;
	}
	b->st = join(a, b->st);
	reset(b);
	return b;
}

Paa split(Arbore k, int poz)
{
	if (k == NIL)
		return { NIL, NIL };
	propagate(k);

	if (poz <= k->st->g) {
		Paa x(split(k->st, poz));
		k->st = x.second;
		reset(k);
		return { x.first, k };
	}
	if (poz == 1 + k->st->g) {
		Paa x({ k->st, k });
		k->st = NIL;
		reset(k);
		return x;
	}
	Paa x(split(k->dr, poz - 1 - k->st->g));
	k->dr = x.first;
	reset(k);
	return { k, x.second };
}

char get_char()
{
	while (!isalnum(buffer[p]))
		p++;
	char ans(buffer[p]);
	next_char();
	return ans;
}

int get_int()
{
	while (!isdigit(buffer[p]))
		next_char();
	int ans(0);
	while (isdigit(buffer[p])) {
		ans *= 10;
		ans += buffer[p] - '0';
		next_char();
	}
	return ans;
}

void next_char()
{
	p++;
	if (!buffer[p]) {
		p = 0;
		fread(buffer, 1, (sizeof buffer) - 1, in);
	}
}