# Cod sursa(job #394072)

Utilizator Data 10 februarie 2010 14:45:34 Secv8 100 cpp done Arhiva de probleme 3.23 kb
``````#include <fstream>
#include <cstdlib>

using namespace std;

const int INF = (1 << 30) + 1;
const int AND = (1 << 30) - 1;

struct Node
{
int cnt, pri, key;
bool rev;
Node *left, *right;

Node() {};

Node(int c, int p, int k, bool rv, Node* l, Node* r) : cnt(c), pri(p), key(k), rev(rv), left(l), right(r) {};
};

int N;
Node *T, *nil;

void expand(Node *T)
{
if (T -> rev)
{
Node *aux = T -> left;
T -> left = T -> right;
T -> right = aux;

T -> left -> rev ^= 1;
T -> right -> rev ^= 1;
T -> rev = 0;
}
}

void recalc(Node *T)
{
T->cnt = T->left->cnt + T->right->cnt + 1;
}

void rotateRight(Node *&T)
{
expand(T);
expand(T -> left);
expand(T -> right);

Node *tmp = T -> left;
T -> left = tmp -> right;
tmp -> right = T;
T = tmp;

recalc(T -> right);
recalc(T);
}

void rotateLeft(Node *&T)
{
expand(T);
expand(T -> left);
expand(T -> right);

Node *tmp = T -> right;
T -> right = tmp -> left;
tmp -> left = T;
T = tmp;

recalc(T -> left);
recalc(T);
}

void balance(Node *&T)
{
if (T->left->pri > T->pri) rotateRight(T);
if (T->right->pri > T->pri) rotateLeft(T);
}

void insert(Node *&T, int pos, int key, int pri)
{
if (T == nil) T = new Node(1, pri, key, 0, nil, nil);
else
{
expand(T);

if (pos <= T->left->cnt + 1) insert(T->left, pos, key, pri);
else insert(T->right, pos - T->left->cnt - 1, key, pri);

balance(T);
recalc(T);
}
}

int search(Node *T, int pos)
{
expand(T);

if (T->left->cnt >= pos) return search(T->left, pos);
if (T->left->cnt + 1 == pos) return T -> key;
return search(T->right, pos - T->left->cnt - 1);
}

void erase(Node *&T, int pos)
{
expand(T);

if (T->left->cnt >= pos) erase(T->left, pos);
else
{
if (T->left->cnt+1 < pos) erase(T->right, pos - T->left->cnt - 1);
else
{
if (T->left == nil && T->right == nil)
{
delete T;
T = nil;
return;
}

if (T->left->pri > T->right->pri)
{
rotateRight(T);
erase(T->right, pos - T->left->cnt - 1);
}
else
{
rotateLeft(T);
erase(T->left, pos);
}
}
}

recalc(T);
}

void split(Node *&T, Node *&T1, Node *&T2, int pos)
{
insert(T, pos, 0, INF);
T1 = T->left;
T2 = T->right;
delete T;
T = nil;
}

void join(Node *&T, Node *T1, Node *T2)
{
T = new Node(0, INF, 0, 0, T1, T2);
recalc(T);
erase(T, T->left->cnt + 1);
}

void SRD(Node *T, ofstream &fout)
{
expand(T);

if (T->left != nil) SRD(T->left, fout);
fout << T->key << " ";
if (T->right != nil) SRD(T->right, fout);
}

int main()
{
ifstream fin("secv8.in");
ofstream fout("secv8.out");

srand(0);

int rev, k, x, y;
char oper;
fin >> N >> rev;

T = nil = new Node(0, 0, 0, 0, NULL, NULL);
nil -> left = nil -> right = nil;

for (int i = 1; i <= N; ++i)
{
fin >> oper;

switch (oper)
{
case 'A':
{
fin >> k;
fout << search(T, k) << "\n";
break;
}
case 'D':
{
fin >> x >> y;
for (int j = x; j <= y; ++j) erase(T, x);
break;
}
case 'I':
{
fin >> x >> y;
insert(T, x, y, (rand() & AND) + 1);
break;
}
case 'R':
{
Node *tmp, *T1, *T2, *T3;

fin >> x >> y;

split(T, tmp, T3, y+1);
split(tmp, T1, T2, x);
T2 -> rev = 1;
join(tmp, T1, T2);
join(T, tmp, T3);
break;
}
}
}

SRD(T, fout);
fout << "\n";

return 0;
}
``````