Cod sursa(job #1152173)

Utilizator ioalexno1Alexandru Bunget ioalexno1 Data 24 martie 2014 16:22:42
Problema Zeap Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.34 kb
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstring>

using namespace std;

set <int> S, dif;
set <int>::iterator it, aux, j, h, q;

int main()
{
    freopen( "zeap.in", "r", stdin );
    freopen( "zeap.out", "w", stdout );

    while( !feof( stdin ) ){

        char line[15];
        memset( line, 0, sizeof( line ) );
        gets( line );
        int l = strlen( line );
        if( l > 0 ){
        if( line[0] != 'M' ){

            int x = 0;
            for( int i = 2; i < l; ++i )
                x = x * 10 + line[i] - 48;
            it = S.find( x );
            if( line[0] == 'I' ){

                if( it == S.end() ) S.insert( x );
                it = S.find( x );
                if( it != S.begin() ){

                    j = it;
                    --j;
                    dif.insert( (*it) - (*j) );
                    h = it;
                    ++h;
                    if( h != S.end() ){

                        dif.insert( (*h) - (*it) );
                        q = dif.find( (*h) - (*j) );
                        if( q != dif.end() ) dif.erase( (*h) - (*j) );
                    }
                }
                else{

                    h = it;
                    ++h;
                    if( h != S.end() )
                        dif.insert( (*j) - (*it) );
                }
            }

            else if( line[0] == 'S' ){


                if( it != S.end() ){

                        if( it != S.begin() ){

                            j = it;
                            --j;
                            q = dif.find( (*it) - (*j) );
                            if( q != dif.end() )
                                dif.erase( (*it) - (*j) );
                            h = it;
                            ++h;
                            if( h != S.end() ){

                                q = dif.find( (*h) - (*it) );
                                if( q != dif.end() )
                                    dif.erase( (*h) - (*it) );
                                dif.insert( (*h) - (*j) );
                            }
                        }
                        else{

                            h = it;
                            ++h;
                            if( h != S.end() ){

                                q = dif.find( (*h) - (*it) );
                                if( q != dif.end() )
                                    dif.erase( (*h) - (*it) );
                            }
                        }
                        S.erase( it );
                }
                else printf( "-1\n" );
            }
            else{

                if( it != S.end() ) printf( "1\n" );
                else printf( "0\n" );
            }
        }
        else {
            if( line[1] == 'A' ){

            if( S.size() > 1 ){

                it = S.end();
                --it;
                aux = S.begin();
                printf( "%d\n", (*it) - (*aux) );
            }
            else printf( "-1\n" );
        }
        else{

            if( S.size() > 1 ){

                it = dif.begin();
                printf( "%d\n", (*it) );
            }
            else printf( "-1\n" );
        }
        }
    }
    }
    fclose( stdin );
    fclose( stdout );
    return 0;
}