Cod sursa(job #3121956)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 16 aprilie 2023 13:46:43
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.89 kb
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <cassert>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <stack>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
bool dbg = 0;
typedef long long ll;
mt19937_64 rng(228);
struct Node {
	int revtag;
	int val;
	int sub;
	ll prio;
	Node* lft;
	Node* rgh;
	Node() {
		revtag = 0;
		val = 0;
		sub = 0;
		prio = rng();
		//cout << "\t\t\t\t generated " << prio << "\n";
		lft = 0;
		rgh = 0;
	}
};
void push(Node* root) {
	assert(root);
	if (root->revtag) {
		swap(root->lft, root->rgh);
		if (root->lft) {
			root->lft->revtag ^= 1;
		}
		if (root->rgh) {
			root->rgh->revtag ^= 1;
		}
		root->revtag = 0;
	}
}
void build(Node* lft, Node* root, Node* rgh) {
	root->lft = lft;
	root->rgh = rgh;
	root->sub = 1;
	if (root->lft) {
		root->sub += root->lft->sub;
	}
	if (root->rgh) {
		root->sub += root->rgh->sub;
	}
	assert(root->revtag == 0);
}
Node* join(Node* a, Node* b) {
	if (a) {
		push(a);
	}
	if (b) {
		push(b);
	}
	if (!a) {
		return b;
	}
	if (!b) {
		return a;
	}
	assert(a);
	assert(b);
	assert(a->sub);
	assert(b->sub);
	if (a->prio > b->prio) {
		build(a->lft, a, join(a->rgh, b));
		return a;
	}
	else {
		assert(a->prio < b->prio);
		build(join(a, b->lft), b, b->rgh);
		return b;
	}
}
int get(Node* a, int p) {
	assert(a);
	push(a);
	assert(1 <= p && p <= a->sub);
	int sublft = (a->lft) ? (a->lft->sub) : (0);
	if (p <= sublft) {
		return get(a->lft, p);
	}
	if (p == sublft + 1) {
		return a->val;
	}
	return get(a->rgh, p - sublft - 1);
}
pair<Node*, Node*> split(Node* a, int inpref) {
	if (!a) {
		assert(inpref == 0);
		return { 0, 0 };
	}
	push(a);
	assert(a);
	assert(a->sub);
	assert(0 <= inpref && inpref <= a->sub);
	if (inpref == 0) {
		return { 0, a };
	}
	if (inpref == a->sub) {
		return { a, 0 };
	}
	int sublft = (a->lft) ? (a->lft->sub) : (0);
	if (inpref <= sublft) {
		auto it = split(a->lft, inpref);
		build(it.second, a, a->rgh);
		return { it.first, a };
	}
	assert(sublft + 1 <= inpref);
	auto it = split(a->rgh, inpref - sublft - 1);
	build(a->lft, a, it.first);
	return { a, it.second };
}
void print_inner(Node* a) {
	push(a);
	assert(a);
	if (a->lft) {
		print_inner(a->lft);
	}
	cout << a->val << " ";
	if (a->rgh) {
		print_inner(a->rgh);
	}
}
Node* root = 0;
void ins(int inpref, int val) {
	Node* b = new Node;
	b->val = val;
	b->sub = 1;
	auto it = split(root, inpref);
	root = join(it.first, b);
	root = join(root, it.second);
}
signed main() {
#ifdef ONPC
	FILE* stream;
	freopen_s(&stream, "input.txt", "r", stdin);
#else
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	freopen("secv8.in", "r", stdin);
	freopen("secv8.out", "w", stdout);
#endif
	int ops, __useless__;
	cin >> ops >> __useless__;
	for (int i = 1; i <= ops; i++) {
		//print(root);
		string s;
		cin >> s;
		if (s == "I") {
			int bef, x;
			cin >> bef >> x;
			bef--;
			ins(bef, x);
			continue;
		}
		if (s == "A") {
			int p;
			cin >> p;
			cout << get(root, p) << "\n";
			continue;
		}
		if (s == "R") {
			int l, r;
			cin >> l >> r;
			auto it = split(root, r);
			auto it2 = split(it.first, l - 1);
			assert(it2.second);
			it2.second->revtag ^= 1;
			root = join(join(it2.first, it2.second), it.second);
			continue;
		}
		if (s == "D") {
			int l, r;
			cin >> l >> r;
			auto it = split(root, r);
			auto it2 = split(it.first, l - 1);
			root = join(it2.first, it.second);
			continue;
		}
	}
	if (root) {
		print_inner(root);
		cout << "\n";
	}
	return 0;
}