Pagini recente » Cod sursa (job #1152145) | Cod sursa (job #2166129) | Cod sursa (job #2291294) | Cod sursa (job #2325060) | Cod sursa (job #1180925)
#include <fstream>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
const int MAXDIF = -1;
const int MINDIF = 1000000009;
struct Zeap{
struct Treap{
int key, priority, maxx, minn, maxdif, mindif;
Treap *Left,*Right;
inline Treap(){
key = priority = 0;
maxx = maxdif = MAXDIF;
minn = mindif = MINDIF;
Left = NULL;
Right = NULL;
}
inline Treap(const int _key,const int _priority,const int _maxx,const int _minn,const int _maxdif,const int _mindif,Treap *_Left,Treap *_Right){
key = _key;
priority = _priority;
maxx = _maxx;
minn = _minn;
maxdif = _maxdif;
mindif = _mindif;
Left = _Left;
Right = _Right;
}
};
Treap *Root,*NIL;
inline void Init(){
srand(time(0));
Root = NIL = new Treap;
}
inline int Random(){
int x = 1 + (rand()%99789)*8;
// cout<<x<<"\n";
return x;
}
inline Zeap(){
Init();
}
inline void Update(Treap *&Root){
Root->maxx = max(Root->key,Root->Right->maxx);
Root->minn = min(Root->key,Root->Left->minn);
Root->maxdif = MAXDIF;
if(Root->maxx >= Root->key && Root->minn <= Root->key && Root->maxx != Root->minn)
Root->maxdif = Root->maxx-Root->minn;
Root->mindif = min(Root->Left->mindif,Root->Right->mindif);
if(Root->Left->maxx != MAXDIF)
Root->mindif = min(Root->mindif,Root->key-Root->Left->maxx);
if(Root->Right->minn != MINDIF)
Root -> mindif = min(Root->mindif,Root->Right->minn - Root->key);
}
inline void RotateLeft(Treap *&Root){
Treap *New = Root->Left;
Root -> Left = New->Right;
New -> Right = Root;
Root = New;
Update(Root->Right);
Update(Root);
}
inline void RotateRight(Treap *&Root){
Treap *New = Root->Right;
Root ->Right = New->Left;
New -> Left = Root;
Root = New;
Update(Root->Left);
Update(Root);
}
inline void Balance(Treap *Root){
if(Root->Left->priority > Root->priority)
RotateLeft(Root);
else
if(Root->Right->priority > Root->priority)
RotateRight(Root);
}
inline bool Search(Treap *&Root,const int x){
if(Root == NIL)
return false;
if(Root->key == x)
return true;
if(x < Root->key)
return Search(Root->Left,x);
return Search(Root->Right,x);
}
inline bool Insert(Treap *&Root,const int x){
if(Root == NIL){
Root = new Treap(x,Random(),x,x,MAXDIF,MINDIF,NIL,NIL);
return true;
}
if(x == Root->key)
return false;
bool ok;
if(x < Root->key)
ok = Insert(Root->Left,x);
else
ok = Insert(Root->Right,x);
if(ok){
Balance(Root);
Update(Root);
}
return ok;
}
inline bool Delete(Treap *&Root,const int x){
if(Root == NIL)
return false;
if(x == Root->key){
if(Root->Left==NIL && Root->Right==NIL){
delete Root;
Root=NIL;
return true;
}
if(Root->Left->priority > Root->Right->priority)
RotateLeft(Root);
else
RotateRight(Root);
return Delete(Root,x);
}
bool ok;
if(x < Root->key)
ok = Delete(Root->Left,x);
else
ok = Delete(Root->Right,x);
if(ok)
Update(Root);
return ok;
}
inline void Add(const int x){
Insert(Root,x);
}
inline bool Erase(const int x){
return Delete(Root,x);
}
inline bool Query(const int x){
return Search(Root,x);
}
inline int GetMAXDIF(){
return Root->maxdif;
}
inline int GetMINDIF(){
if(Root->mindif == MINDIF)
return -1;
return Root->mindif;
}
};
Zeap Z;
int main(){
int x;
ifstream f("zeap.in");
ofstream g("zeap.out");
string s;
while(f>>s){
if(s=="I"){
f >> x;
//g<<"insert "<<x<<"\n";
Z.Add(x);
}
else{
if(s=="S"){
f >> x;
if(!Z.Erase(x))
g<<"-1\n";
}
else{
if(s=="C"){
f >> x;
g<<Z.Query(x)<<"\n";
}
else
if(s=="MAX"){
// g<<"cauta maxim\n";
g<<Z.GetMAXDIF()<<"\n";
}
else
g<<Z.GetMINDIF()<<"\n";
}
}
}
return 0;
}