Cod sursa(job #786671)

Utilizator visanrVisan Radu visanr Data 11 septembrie 2012 18:47:54
Problema Zeap Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.74 kb
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <algorithm>
using namespace std;


set<int> S;
map<int, int> M;
int N, nr;
char s[30];
set<int> :: iterator it, now;


int main()
{
    freopen("zeap.in", "r", stdin);
    freopen("zeap.out", "w", stdout);
    int i;
    while(gets(s))
    {
             if(s[0] == 'I')
             {
                     nr = 0;
                     for(i = 2; isdigit(s[i]); i++)
                           nr = nr * 10 + s[i] - '0';
                     if(S.find(nr) == S.end())
                     {
                                   S.insert(nr);
                                   it = S.upper_bound(nr);
                                   if(it != S.end())
                                   {
                                         now = it;
                                         -- now;
                                         M[*it - nr] ++;
                                         if(now == S.begin()) continue;
                                         -- now;
                                         -- M[*it - *now];
                                         if(M[*it - *now] == 0) M.erase(*it - *now);
                                   }
                                   -- it;
                                   if(it == S.begin()) continue;
                                   -- it;
                                   M[nr - *it] ++;
                     }
                     continue;
             }
             if(s[0] == 'S')
             {
                     nr = 0;
                     for(i = 2; isdigit(s[i]); i++)
                           nr = nr * 10 + s[i] - '0';
                     it = S.find(nr);
                     if(it == S.end()) printf("-1\n");
                     else
                     {
                         S.erase(it);
                         if(S.size() == 0) continue;
                         now = S.upper_bound(nr);
                         if(now != S.end())
                         {
                                --M[*now - nr];
                                if(M[*now - nr] == 0) M.erase(*now - nr);
                         }
                         if(now != S.begin())
                         {
                                -- now;
                                -- M[nr - *now];
                                if(M[nr - *now] == 0) M.erase(nr - *now);
                                int crt = *now;
                                ++ now;
                                if(now != S.end())
                                       M[*now - crt] ++;
                         }
                     }
                     continue;
             }
             if(s[0] == 'C')
             {
                     nr = 0;
                     for(i = 2; isdigit(s[i]); i++)
                           nr = nr * 10 + s[i] - '0';
                     it = S.find(nr);
                     if(it != S.end()) printf("1\n");
                     else printf("0\n");
                     continue;
             }
             if(s[1] == 'I')
             {
                     if(S.size() >= 2) printf("%i\n", (*M.begin()).first);
                     else printf("-1\n");
                     continue;
             }
             if(s[1] == 'A')
             {
                     if(S.size() >= 2) 
                     {
                                 it = S.end();
                                 -- it;
                                 printf("%i\n", (*it) - (*S.begin()));
                     }else
                     {
                          printf("-1\n");
                     }
                     continue;
             }
    }
    return 0;
}