Pagini recente » Cod sursa (job #2387261) | Cod sursa (job #1174832) | Cod sursa (job #502209) | Cod sursa (job #1267553) | Cod sursa (job #1188729)
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
struct treap{
struct Treap{
int key,priority;
Treap *Left,*Right;
Treap(){
key = priority = 0;
Left = NULL;
Right = NULL;
}
inline Treap(const int _key,const int _priority,Treap *_Left,Treap *_Right){
key = _key;
priority = _priority;
Left = _Left;
Right = _Right;
}
};
Treap *Root,*NIL;
inline void Init(){
srand(time(0));
NIL = new Treap();
NIL->Left = NIL->Right = NIL;
Root = new Treap(0,0,NIL,NIL);
}
inline int Random(){
return 1 + (rand()%100)*(rand()%100) + (rand()%4)* (rand()%5000000);
}
inline bool Search(Treap *&Root,const int x){
if(Root == NIL)
return false;
if(Root->key == x)
return true;
if(Root->key > x)
return Search(Root->Left,x);
return Search(Root->Right,x);
}
inline void RotateLeft(Treap *&Root){
Treap *New = Root->Left;
Root -> Left = New->Right;
New -> Right = Root;
Root = New;
}
inline void RotateRight(Treap *&Root){
Treap *New = Root->Right;
Root->Right = New->Left;
New -> Left = Root;
Root = New;
}
inline void Balance(Treap *&Root){
if(Root->Left->priority > Root->priority)
RotateLeft(Root);
else
if(Root->Right->priority> Root->priority)
RotateRight(Root);
}
inline void Insert(Treap *&Root,const int x){
if(Root==NIL){
Root = new Treap(x,Random(),NIL,NIL);
return;
}
if(Root->key > x)
Insert(Root->Left,x);
else
if(Root-> key < x)
Insert(Root->Right,x);
Balance(Root);
}
inline void Delete(Treap *&Root,const int x){
if(Root == NIL)
return ;
if(Root->key > x)
Delete(Root->Left,x);
else
if(Root->key < x)
Delete(Root->Right,x);
else{
if(Root->Left == NIL && Root->Right==NIL){
Root = NIL;
delete Root;
}
else{
if(Root->Left->priority > Root->Right->priority)
RotateLeft(Root);
else
RotateRight(Root);
Delete(Root,x);
}
}
}
inline void Add(const int x){
Insert(Root,x);
}
inline void Erase(const int x){
Delete(Root,x);
}
inline bool Query(const int x){
return Search(Root,x);
}
};
treap Tree;
int main(){
int T,x;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
Tree.Init();
f >> T;
while(T--){
int op;
f >> op;
if(op==1){
f >> x;
Tree.Add(x);
}
else
if(op==2){
f >> x;
Tree.Erase(x);
}
else{
f >> x;
g<<Tree.Query(x)<<"\n";
}
}
f.close();
g.close();
return 0;
}