Pagini recente » Cod sursa (job #483454) | Istoria paginii runda/piscot1024 | Istoria paginii runda/123456789012 | Cod sursa (job #1798991) | Cod sursa (job #1190770)
#include<fstream>
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
const int INF = (1LL<<31)-1;
struct Node
{
int key,priority;
Node *left_son,*right_son;
Node()
{
key = 0;
priority = 0;
left_son = NULL;
right_son = NULL;
}
Node(int _key,int _priority,Node* _left_son,Node* _right_son)
{
key = _key;
priority = _priority;
left_son = _left_son;
right_son = _right_son;
}
} *T,*NIL;
int random_nr()
{
return rand()%INF+1;
}
void create(Node* &Q)
{
srand(time(NULL));
Q = NIL = new Node;
}
int search(Node* Q,int x)
{
if(Q == NIL) return 0;
if(Q->key > x) return search(Q->left_son,x);
else if(Q->key < x) return search(Q->right_son,x);
return 1;
}
void rotate_left(Node* &Q)
{
Node* S = Q->left_son;
Q->left_son = S->right_son;
S->right_son = Q;
Q = S;
}
void rotate_right(Node* &Q)
{
Node* S = Q->right_son;
Q->right_son = S->left_son;
S->left_son = Q;
Q = S;
}
void balance(Node* &Q)
{
if(Q->priority < Q->left_son->priority) rotate_left(Q);
else if(Q->priority < Q->right_son->priority) rotate_right(Q);
}
void insert(Node* &Q,int x,int pr)
{
if(Q == NIL)
{
Q = new Node(x,pr,NIL,NIL);
return;
}
if(Q->key > x) insert(Q->left_son,x,pr);
else if(Q->key < x) insert(Q->right_son,x,pr);
balance(Q);
}
void erase(Node* &Q,int x)
{
if(Q == NIL) return;
if(Q->key > x) erase(Q->left_son,x);
else if(Q->key < x) erase(Q->right_son,x);
else
{
if(Q->left_son == NIL && Q->right_son == NIL)
{
delete Q;
Q = NIL;
return;
}
if(Q->left_son == NIL)
{
Q = Q->right_son;
return;
}
if(Q->right_son == NIL)
{
Q = Q->left_son;
return;
}
Q = Q->right_son;
if(Q->left_son->priority > Q->priority) rotate_left(Q);
}
}
void split(Node* &Q,Node* &Left,Node* &Right,int x)
{
insert(Q,x,INF);
Left = Q->left_son;
Right = Q->right_son;
delete Q;
Q = NIL;
}
void join(Node* &Q,Node* &Left,Node* &Right,int x)
{
Q = new Node(x,0,Left,Right);
erase(Q,0);
}
int N;
int main()
{
int op,x;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
fin>>N;
create(T);
for(; N; --N)
{
fin>>op>>x;
if(op==3) fout<<search(T,x)<<'\n';
if(op==1) insert(T,x,random_nr());
if(op==2 && search(T,x)) erase(T,x);
}
return 0;
}