Cod sursa(job #1991995)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 18 iunie 2017 23:16:30
Problema Zeap Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Semestrul 2 Marime 2.84 kb
#include <cstdio>
#include <set>

using namespace std;

set <int> s;
multiset <int> dif;

int main()
{
    freopen("zeap.in", "r", stdin);
    freopen("zeap.out", "w", stdout);
    char c;
    while(scanf("%c", &c) != EOF)
    {
        if(c == 'I')
        {
            int x;
            scanf("%d\n", &x);
            if(s.size() == 0)
            {
                s.insert(x);
                continue;
            }
            set <int> :: iterator it2 = s.lower_bound(x), it1;
            it1 = it2; it1--;
            if(it2 != s.end() && (*it2) == x) continue;
            if(it2 == s.begin())
                dif.insert((*it2) - x);
            else if(it2 == s.end())
                dif.insert(x - (*it1));
            else
            {
                dif.erase(dif.find((*it2) - (*it1)));
                dif.insert((*it2) - x);
                dif.insert(x - (*it1));
            }
            s.insert(x);
        }
        else if(c == 'S')
        {
            int x;
            scanf("%d\n", &x);
            if(s.size() == 0)
            {
                printf("-1\n");
                continue;
            }
            set <int> :: iterator it2 = s.lower_bound(x), it1, it3;
            it1 = it2; it1--;
            it3 = it2; it3++;
            if(it2 == s.end() || (*it2) != x)
            {
                printf("-1\n");
                continue;
            }
            if(s.size() == 1)
            {
                s.erase(x);
                continue;
            }
            if(it2 == s.begin())
                dif.erase(dif.find((*it3) - x));
            else if(it3 == s.end())
                dif.erase(x - (*it1));
            else
            {
                dif.insert((*it3) - (*it1));
                dif.erase(dif.find((*it3) - x));
                dif.erase(dif.find(x - (*it1)));
            }
            s.erase(x);
        }
        else if(c == 'C')
        {
            int x;
            scanf("%d\n", &x);
            printf("%d\n", s.find(x)!=s.end() ? 1 : 0);
        }
        else
        {
            scanf("%c", &c);
            if(c == 'A')
            {
                scanf("X\n");
                if(s.size() <= 1)
                {
                    printf("-1\n");
                    continue;
                }
                set <int> :: iterator it1 = s.begin(), it2 = s.end();
                it2--;
                printf("%d\n", (*it2) - (*it1));
            }
            else
            {
                scanf("N\n");
                if(s.size() <= 1)
                {
                    printf("-1\n");
                    continue;
                }
                multiset <int> :: iterator it = dif.begin();
                printf("%d\n", (*it));
            }
        }
    }
    return 0;
}