#include <fstream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#define INF 2000000001
#define DIM 100
#define infile "zeap.in"
#define outfile "zeap.out"
using namespace std;
ifstream f(infile);
ofstream g(outfile);
char BUFFER[DIM];
int n, x;
struct treap {
int key, priority, minim, maxim, dif_min;
treap *left, *right;
treap() {};
treap(int key, int priority, int minim, int maxim, int dif_min, treap *left, treap *right) {
this->key = key;
this->priority = priority;
this->minim = minim;
this->maxim = maxim;
this->dif_min = dif_min;
this->left = left;
this->right = right;
}
} *Root, *Empty;
inline int modul(int a) {
return (a > 0 ? a : -a);
}
bool Search(treap *nod, int key) {
if (nod == Empty)
return false;
if (key == nod->key)
return true;
if (key > nod->key)
return Search(nod->right, key);
return Search(nod->left, key);
}
void Update(treap *&nod) {
nod->minim = std::min(nod->key, nod->left->minim);
nod->maxim = std::max(nod->key, nod->right->maxim);
nod->dif_min = std::min( std::min(nod->right->dif_min, nod->left->dif_min), std::min( modul(nod->key - nod->left->maxim), modul(nod->key - nod->right->minim) ) );
}
void Rotate_Left(treap *&nod) {
treap *t = nod->left;
nod->left = t->right;
t->right = nod;
nod = t;
if (nod != Empty && nod->right != Empty)
Update(nod->right);
}
void Rotate_Right(treap *&nod) {
treap *t = nod->right;
nod->right = t->left;
t->left = nod;
nod = t;
if (nod != Empty && nod->left != Empty)
Update(nod->left);
}
void Balance(treap *&nod) {
if (nod->left->priority > nod->priority)
Rotate_Left(nod);
else
if (nod->right->priority > nod->priority)
Rotate_Right(nod);
Update(nod);
}
void Insert(treap *&nod, int key, int priority) {
if (nod == Empty) {
nod = new treap(key, priority, key, key, INF, Empty, Empty);
++n;
return;
}
if (key < nod->key)
Insert(nod->left, key, priority);
else
if (key > nod->key)
Insert(nod->right, key, priority);
Balance(nod);
}
void Erase(treap *&nod, int key) {
if (nod == Empty)
return;
if (key < nod->key)
Erase(nod->left, key);
else
if (key > nod->key)
Erase(nod->right, key);
else {
if (nod->left == Empty && nod->right == Empty) {
delete nod;
nod = Empty;
}
else {
(nod->left->priority > nod->right->priority) ? (Rotate_Left(nod), Erase(nod->right, key)) : (Rotate_Right(nod), Erase(nod->left, key));
}
}
if (nod != Empty)
Update(nod);
}
int main() {
Root = Empty = new treap(0, 0, INF, -INF, INF, NULL, NULL);
srand(unsigned(time(NULL)));
while (f >> BUFFER) {
if (BUFFER[0] == 'I') {
f >> x;
Insert(Root, x, rand() % (INF - 1) + 1);
continue;
}
if (BUFFER[0] == 'S') {
f >> x;
if (!Search(Root, x)) {
g << "-1\n";
continue;
}
Erase(Root, x);
--n;
continue;
}
if (BUFFER[0] == 'C') {
f >> x;
g << Search(Root, x) << "\n";
continue;
}
if (BUFFER[1] == 'A') {
if (n < 2)
g << "-1\n";
else
g << Root->maxim - Root->minim << "\n";
continue;
}
if (n < 2)
g << "-1\n";
else
g << Root->dif_min << "\n";
}
return 0;
}
//Trust me, I'm the Doctor!