#include <bits/stdc++.h>
#define for0(i,n) for(int i=0; i<n; i++)
#define for1(i,n) for(int i=1; i<=n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A,x) memset(A, x, sizeof(A))
#define cpy(A,B) memcpy(A, B, sizeof(B))
#define g(s) getline(cin, s) ///ai grija la fin/cin ///
#define FASTIO ios_base::sync_with_stdio(0)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int CHARMAX = (1 << 10);
const int INFINT = 2000000000;
const ll INFLL = (1LL << 62);
class InParsare
{
private:
FILE *fin;
char *buffer;
size_t index_of_buffer;
char read_character()
{
index_of_buffer++;
if(index_of_buffer == CHARMAX)
{
fread(buffer, 1, CHARMAX, fin);
index_of_buffer = 0;
}
return buffer[index_of_buffer];
}
public:
InParsare(const char *name)
{
fin = fopen(name, "r");
buffer = new char[CHARMAX]();
index_of_buffer = CHARMAX - 1;
}
template<class T>
InParsare &operator >> (T &n)
{
char c;
while(!isdigit(c = read_character()) && c != '-');
int sgn = 1;
if(c == '-')
{
n = 0;
sgn = -1;
}
else n = c - '0';
while(isdigit(c = read_character()))
n = n * 10 + c - '0';
n *= sgn;
return *this;
}
};
class OutParsare
{
private:
FILE *fout;
char *buffer;
size_t index_of_buffer;
void write_character(char character)
{
if(index_of_buffer == CHARMAX)
{
fwrite(buffer, 1, CHARMAX, fout);
index_of_buffer = 0;
buffer[index_of_buffer++] = character;
}
else buffer[index_of_buffer++] = character;
}
public:
OutParsare(const char *name)
{
fout = fopen(name, "w");
buffer = new char[CHARMAX]();
index_of_buffer = 0;
}
~OutParsare()
{
fwrite(buffer, 1, index_of_buffer, fout);
fclose(fout);
}
template<class T>
OutParsare &operator << (T n)
{
if(typeid(T).name() == typeid(char).name())
{
write_character(n);
return *this;
}
if(n <= 9)
write_character(n + '0');
else
{
(*this) << (n / 10);
write_character(n % 10 + '0');
}
return *this;
}
};
//InParsare fin("nume.in"); /// merg numai pt numere
//OutParsare fout("nume.out"); /// merg numai pt numere + caractere individuale, nu stringuri
ifstream fin("zeap.in");
ofstream fout("zeap.out");
struct node
{
int mmin, mmax, difmin, key, priority;
node *left, *right;
};
node* newNode(int key)
{
node *ret = new node();
ret -> mmin = ret -> mmax = ret -> key = key;
ret -> difmin = INFINT;
ret -> priority = rand();
ret -> left = ret -> right = NULL;
return ret;
}
void calculations(node *&nod)
{
nod -> mmin = nod -> mmax = nod -> key;
nod -> difmin = INFINT;
if(nod -> left)
{
nod -> mmin = min(nod -> mmin , nod -> left -> mmin);
nod -> mmax = max(nod -> mmax, nod -> left -> mmax);
nod -> difmin = min(nod -> difmin, nod -> left -> difmin);
nod -> difmin = min(nod -> difmin, nod -> key - nod -> left -> mmax);
}
if(nod -> right)
{
nod -> mmin = min(nod -> mmin , nod -> right -> mmin);
nod -> mmax = max(nod -> mmax, nod -> right -> mmax);
nod -> difmin = min(nod -> difmin, nod -> right -> difmin);
nod -> difmin = min(nod -> difmin, nod -> right -> mmin - nod -> key);
}
}
node* rotateRight(node *y)
{
node *x = y -> left , *T2 = x -> right;
x -> right = y;
y -> left = T2;
calculations(y);
calculations(x);
return x;
}
node* rotateLeft(node *x)
{
node *y = x -> right , *T2 = y -> left;
y -> left = x;
x -> right = T2;
calculations(x);
calculations(y);
return y;
}
node* insertion(node *nod, int key)
{
if(nod == NULL)
return newNode(key);
if(key < nod -> key)
{
nod -> left = insertion(nod -> left, key);
if(nod -> left -> priority > nod -> priority)
nod = rotateRight(nod);
}
if(key > nod -> key)
{
nod -> right = insertion(nod -> right, key);
if(nod -> right -> priority > nod -> priority)
nod = rotateLeft(nod);
}
calculations(nod);
return nod;
}
node* removal(node *nod, int key)
{
if(nod == NULL)
return nod;
if(key < nod -> key)
nod -> left = removal(nod -> left, key);
else if(key > nod -> key)
nod -> right = removal(nod -> right, key);
else if(nod -> left == NULL)
{
node *temp = nod -> right;
delete(nod);
nod = temp;
}
else if(nod -> right == NULL)
{
node *temp = nod -> left;
delete(nod);
nod = temp;
}
else
{
if(nod -> left -> priority > nod -> right -> priority)
{
nod = rotateRight(nod);
nod -> right = removal(nod -> right, key);
}
else
{
nod = rotateLeft(nod);
nod -> left = removal(nod -> left, key);
}
}
if(nod == NULL)
return nod;
calculations(nod);
return nod;
}
unordered_map<int, bool> contor;
int main()
{
node *root = NULL;
string op;
int a;
while(fin >> op)
{
if(op == "I")
{
fin >> a;
root = insertion(root, a);
contor[a] = 1;
}
else if(op == "S")
{
fin >> a;
if(contor[a])
{
contor[a] = 0;
root = removal(root, a);
}
else fout << "-1\n";
}
else if(op == "C")
{
fin >> a;
fout << contor[a] << '\n';
}
else if(op == "MAX")
{
if(root == NULL)
fout << "-1\n";
else fout << (root -> mmax - root -> mmin == 0 ? -1 : root -> mmax - root -> mmin) << '\n';
}
else if(op == "MIN")
{
if(root == NULL)
fout << "-1\n";
else fout << (root -> difmin == INFINT ? -1 : root -> difmin) << '\n';
}
}
return 0;
}