Cod sursa(job #1194182)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 3 iunie 2014 02:43:37
Problema Zeap Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Semestrul 2 Marime 3.55 kb
#include<fstream>
#include<string>
#include<set>
using namespace std;
ifstream fi("zeap.in");
ofstream fo("zeap.out");

const int inf = (1LL<<31)-1;

pair <set<int>::iterator,bool> aux;
multiset <int> m_set;
set <int>::iterator it;
set <int> s;
int i,x,a,b;
char c;
string linie;
int lung;

int main(){
    
    s.insert(0);
    s.insert(inf);
    
    while(getline(fi,linie)){
                 lung=linie.length();
                 
                 if(linie[0]=='M') c=linie[2];
                 else{
                      c=linie[0]; x=0;
                      for(i=2;i<lung;i++) x=x*10+linie[i]-'0';
                     }
                 if(c=='I')
                   {
                    aux=s.insert(x);
                    if(aux.second){
                                      it=aux.first;
                                      it--;
                                      a=*it;
                                      it++;
                                      it++;
                                      b=*it;
                                      
                                      if(a>0) m_set.insert(x-a);
                                      if(b<inf) m_set.insert(b-x);
                                      if(a>0 && b<inf) m_set.insert(b-a);
                                     }
                    continue;
                   }          
                 else if(c=='S')
                        {
                         it=s.find(x);
                         if(it!=s.end()){
                                           it--;
                                           a=*it;
                                           it++;
                                           it++;
                                           b=*it;
                                           it--;
                                           
                                           if(a>0) m_set.erase(m_set.find(x-a));
                                           if(b<inf) m_set.erase(m_set.find(b-x));
                                           if(a>0 && b<inf) m_set.insert(b-a);
                                           
                                           s.erase(it);
                                          }
                         else fo<<"-1\n";   
                         continue;     
                        }
                 else if(c=='C'){
                                 if(s.find(x)!=s.end()) fo<<"1\n";
                                 else fo<<"0\n";
                                 continue;
                                }
                 else{
                      if(c=='X')
                        {
                         if(s.size()<=3) fo<<"-1\n";
                         else{
                              it=s.end();
                              it--;
                              it--;
                              a=*it;
                              it=s.begin();
                              it++;
                              b=*it;
                              
                              fo<<a-b<<"\n";
                             }
                         continue;
                        } 
                      else if(c=='N')
                             {
                              if(s.size()<=3) fo<<"-1\n";
                              else fo<<*(m_set.begin())<<"\n";
                             }
                      continue;
                     }
    
                }
    
    fi.close();
    fo.close();
    return 0;
}