Cod sursa(job #1961517)

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

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

char aux[20];
char afbuffer[1 << 17];
int afp;
inline void printchar(char c);
inline void printint(int c);

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

inline void reset(Arbore k);
inline void propagate(Arbore k);
inline Arbore insert(Arbore k, int val, int poz);
inline Arbore erase(Arbore k, int st, int dr);
inline 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");
	NIL = new Treap();
	NIL->st = NIL->dr = NIL;
	NIL->g = 0;
	Arbore k = NIL;

	register int n(get_int()), q(get_int()), a, b;
	register char op;

	while (n--) {
		a = b = 0;
		op = get_char();
		if (op == 'I') {
			a = get_int(), b = get_int();
			k = insert(k, b, a);
		}
		else if (op == 'A') {
			a = get_int();
			printint(access(k, a));
			printchar('\n');
		}
		else if (op == 'R') {
			a = get_int(), b = get_int();
			k = rotate(k, a, b);
		}
		else {
			a = get_int(), b = get_int();
			k = erase(k, a, b);
		}
	}
	inorder(k);
	fprintf(out, "%s", afbuffer);
	return 0;
}


void inorder(Arbore k)
{
	propagate(k);
	if (k == NIL)
		return;
	inorder(k->st);
	printint(k->val);
	printchar(' ');
	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]))
		next_char();
	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()
{
	if (++p == bsize) {
		p = 0;
		fread(buffer, 1, bsize, in);
	}
}

inline void printchar(char c)
{
	afbuffer[afp++] = c;
	if (afp == bsize - 1) {
		fprintf(out, "%s", afbuffer);
		memset(afbuffer, 0, bsize);
		afp = 0;
	}
}

inline void printint(int c)
{
	int q(0);
	while (c) {
		aux[q++] = c % 10 + '0';
		c /= 10;
	}
	while (q--)
		printchar(aux[q]);
}