Cod sursa(job #1148682)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 20 martie 2014 23:31:18
Problema Zeap Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Semestrul 2 Marime 2.37 kb
#include<cstdio>
#include<set>

using namespace std;

int x,poz;
char T[20],*p;

set<int> S;
multiset<int> D;
set<int>::iterator it,it2,it3;

int main()
{
    freopen("zeap.in","r",stdin);
    freopen("zeap.out","w",stdout);
    for(;fgets(T,20,stdin);)
    {
        if(T[0]=='I')
        {
            p=T+2; x=0;
            while(*p>='0' && *p<='9') x=x*10+*p-'0',p++;
            it=S.find(x);
            if(it==S.end())
            {
                S.insert(x); it=S.find(x);
                if(S.size()>1)
                {
                    it3=it; it3++;
                    it2=it; it2--;
                    if(it==S.begin()) D.insert(*it3-*it);
                    else if(it3==S.end()) D.insert(*it-*it2);
                    else
                    {
                        D.erase(D.find(*it3-*it2));
                        D.insert(*it-*it2);
                        D.insert(*it3-*it);
                    }
                }
            }
        }
        else if(T[0]=='S')
        {
            p=T+2; x=0;
            while(*p>='0' && *p<='9') x=x*10+*p-'0',p++;
            it=S.find(x);
            if(it==S.end()) printf("-1\n");
            else
            {
                if(S.size()>1)
                {
                    it3=it; it3++;
                    it2=it; it2--;
                    if(it==S.begin()) D.erase(D.find(*it3-*it));
                    else if(it3==S.end()) D.erase(D.find(*it-*it2));
                    else
                    {
                        D.insert(*it3-*it2);
                        D.erase(D.find(*it-*it2));
                        D.erase(D.find(*it3-*it));
                    }
                }
                S.erase(it);
            }
        }
        else if(T[0]=='C')
        {
            p=T+2; x=0;
            while(*p>='0' && *p<='9') x=x*10+*p-'0',p++;
            it=S.find(x);
            if(it==S.end()) printf("0\n"); else printf("1\n");
        }
        else if(T[1]=='A')
        {
            if(S.size()<2) printf("-1\n");
            else
            {
                it3=S.end(); it3--;
                it2=S.begin();
                printf("%d\n",*it3-*it2);
            }
        }
        else
        {
            if(S.size()<2) printf("-1\n");
            else printf("%d\n",*D.begin());
        }
    }
    return 0;
}