Pagini recente » Cod sursa (job #2823660) | Cod sursa (job #939141) | Cod sursa (job #3290670) | Cod sursa (job #76310) | Cod sursa (job #1409284)
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
Node *st, *dr, *p;
explicit Node() : key(0), st(NULL), dr(NULL), p(NULL) {
}
Node(const int k) : key(k), st(NULL), dr(NULL), p(NULL) {
}
};
Node *root;
bool isRoot(Node*& X)
{
return (X->p == NULL || (X->p->st != X && X->p->dr != X));
}
Node* rotateToRight(Node* X)
{
Node *B = X->st;
Node *Y = X->p;
X->p = Y->p;
B->p = Y;
Y->p = X;
X->dr = Y;
Y->st = B;
return X;
}
Node* rotateToLeft(Node* Y)
{
Node *B = Y->st;
Node *X = Y->p;
Y->p = X->p;
B->p = X;
X->p = Y;
Y->st = X;
X->dr = B;
return Y;
}
Node* splay(Node* u)
{
while (!isRoot(u))
{
Node *p = u->p;
if (p->st == u)
u = rotateToRight(u);
else
u = rotateToLeft(u);
}
return u;
}
bool insert(int key)
{
Node *newNode = root;
Node *last = NULL;
while (newNode)
{
last = newNode;
if (newNode->key == key)
return false;
if (key < newNode->key)
newNode = newNode->st;
else
newNode = newNode->dr;
}
newNode = new Node(key);
newNode->p = last;
root = splay(newNode);
return true;
}
Node* findNode(int key)
{
Node *aux = root;
while (aux)
{
if (aux->key == key)
break;
if (key < aux->key)
aux = aux->st;
else
aux = aux->dr;
}
return aux;
}
bool find(int key)
{
Node *aux = findNode(key);
if (!aux)
return false;
root = splay(aux);
return true;
}
bool erase(int key)
{
Node *aux = findNode(key);
if (!aux)
return false;
root = splay(aux);
Node *leftSon = root->st;
Node *rightSon = root->dr;
if (leftSon == NULL)
{
Node *toErase = root;
delete toErase;
root = rightSon;
return true;
}
Node *newRoot = leftSon;
while (newRoot->dr != NULL)
newRoot = newRoot->dr;
Node *tata = newRoot->p;
newRoot->p = NULL;
if (tata->st == newRoot)
tata->st = NULL;
else
tata->dr = NULL;
root->key = newRoot->key;
return true;
}
int main()
{
ifstream in("hashuri.in");
ofstream out("hashuri.out");
ios_base::sync_with_stdio( false );
int N;
in >> N;
while ( N-- )
{
int tip, x;
in >> tip >> x;
switch(tip)
{
case 1: insert(x); break;
case 2: erase(x); break;
case 3: out << find(x) << "\n"; break;
default: cerr << "Error\n";
}
}
return 0;
}