Pagini recente » Cod sursa (job #941328) | Istoria paginii utilizator/sbenghici_ | Cod sursa (job #189548) | Cod sursa (job #1677976) | Cod sursa (job #1342078)
#include <fstream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#define nmax 300005
#define inf 1<<30
using namespace std;
ifstream f("zeap.in");
ofstream g("zeap.out");
set <int> se;
map <int , int > mapi;
int n=300000,n1,m,poz;
char s[20];
struct element{
bool ok;
int mi,ma;
int difmin,difmax;
;};
int a[nmax];
element v[nmax*5];
int modul(int x)
{
if (x<0) return -x;
return x;
}
void update(int st, int dr, int nod)
{
if (st==dr) {
if (a[st]==inf)
v[nod].ok=false;
else
v[nod].ok=true;
v[nod].mi=a[st];
v[nod].ma=a[st];
v[nod].difmin=inf;
v[nod].difmax=-inf;
return;
}
int mid=(st+dr)>>1,x;
if (poz<=mid) update(st, mid , nod * 2 );
if (poz>mid) update(mid+1 , dr , nod *2 +1 );
if (v[nod*2].ok==true||v[nod*2+1].ok==true)
v[nod].ok=true;
else
{v[nod].ok=false;
return;
}
v[nod].mi=inf;
v[nod].ma=-inf;
if (v[nod * 2].ok==true)
v[nod].mi=min(v[nod].mi, v[ nod *2] . mi);
if (v[nod * 2+1].ok==true)
v[nod].mi=min(v[nod].mi, v[nod * 2 +1].mi);
if (v[nod * 2].ok==true)
v[nod].ma=max(v[nod].ma, v[nod * 2].ma);
if (v[nod * 2+1].ok==true)
v[nod].ma=max(v[nod].ma, v[nod * 2 +1].ma);
if (v[nod].ma==v[nod].mi)
v[nod].difmax=-inf;
else
v[nod].difmax=modul(v[nod].ma-v[nod].mi);
x=1<<30;
if (v[nod *2].ok==true)
x=min(x,modul(v[nod * 2].difmin));
if (v[nod *2+1].ok==true)
x=min(x,modul(v[nod *2 +1].difmin));
if (v[nod *2].ok==true&&v[nod * 2+1].ok==true) {
x=min(x,modul(v[nod * 2].ma-v[nod * 2 + 1].ma));
x=min(x,modul(v[nod * 2].mi-v[nod * 2 + 1].mi));
}
v[nod].difmin=x;
}
void solve()
{
if (strcmp(s,"MIN")==0){
if (v[1].difmin==inf)
g<<-1<<'\n';
else
g<<v[1].difmin<<'\n';
return;
}
if (strcmp(s,"MAX")==0){
if (v[1].difmax==-inf)
g<<-1<<'\n';
else
g<<v[1].difmax<<'\n';
return;
}
int x=0;
for (int i=2;i<m;i++) x=x*10+s[i]-'0';
if (s[0]=='C') {
g<<se.count(x)<<'\n';
return;
}
if (s[0]=='I') {
if (se.count(x)!=0)
return;
n1++;
a[n1]=x;
se.insert(x);
mapi[x]=n1;
poz=n1;
update(1,n,1);
return;
}
if (s[0]=='S') {
if (se.count(x)==0) {
g<<-1<<'\n';
return;
}
poz=mapi[x];
a[poz]=inf;
se.erase(x);
mapi.erase(x);
update(1,n,1);
}
}
int main()
{
int i,j;
while (1) {
f.getline(s,20);
m=strlen(s);
if (m==0)
break;
solve();
}
return 0;
}