Cod sursa(job #2959487)

Utilizator OvidRata Ovidiu Ovid Data 31 decembrie 2022 14:38:51
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.3 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;

ifstream fin("zeap.in"); ofstream fout("zeap.out");
#define cout fout
#define cin fin

multiset<int> dif;
set<int> st;


int32_t main(){
    INIT



    string s;
    while(cin>>s){
        int x;
        if(s=="I"){
            cin>>x;

            if( (st.size()>0) && (st.find(x)!=st.end()) ){continue;}
            st.insert(x);
            auto it1=st.find(x);
            if(it1!=st.begin()){it1--; dif.insert(x-*it1); it1++;}
            auto it2=st.find(x);
            if(it2!=st.end()){it2++;if(it2!=st.end()){dif.insert(*it2-x);} it2--;}
            if(  (it1!=st.begin()) && (it2!=st.end()) ){
                it1--; it2++;
                if(it2!=st.end()){
                    dif.erase(dif.find(*it2-*it1) );}
            }

        }
        if(s=="S"){
            cin>>x;
            if(st.size()==0){
                cout<<"-1\n"; continue;
            }
            if( (st.find(x)==st.end()) ){
                cout<<"-1\n"; continue;
            }
            auto it1=st.find(x);
            if(it1!=st.begin()){it1--; dif.erase(dif.find(x-*it1) ); it1++;}
            auto it2=st.find(x);
            if(it2!=st.end()){it2++;if(it2!=st.end()){dif.erase(dif.find(*it2-x) );} it2--;}
            if(  (it1!=st.begin()) && (it2!=st.end()) ){
                it1--; it2++;
                if(it2!=st.end()){dif.insert(*it2-*it1);}
            }
            st.erase(x);
        }
        if(s=="C"){
            cin>>x;
            cout<<((st.size()>0) && (st.find(x)!=st.end()) )<<"\n";
        }
        if(s=="MIN"){
            if(dif.size()==0){
                cout<<"-1\n";
            }
            else{
                cout<<*dif.begin()<<"\n";
            }
        }
        if(s=="MAX"){
            if(st.size()<2){
                cout<<"-1\n";
            }
            else{
                auto it=st.end(); it--;
                cout<<(*it-*st.begin())<<"\n";
            }
        }

    }



    return 0;
}