Cod sursa(job #2673414)

Utilizator OvidRata Ovidiu Ovid Data 16 noiembrie 2020 19:09:22
Problema Zeap Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.22 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;
int inf=1e9+5;

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


struct it{

    int x, y, cnt;

        it *l, *r;

    it(int x, int y){
        this->x=x;
        this->y=y;
        this->cnt=1;
        this->l=NULL;
        this->r=NULL;
    }

} *R, *D;





void merge(it *&t, it*l, it*r){
    if((!l)||(!r)){t=(l)?(l):(r); return;}

    if(l->y>r->y){
        merge(l->r, l->r, r); t=l;
    }
    else{
        merge(r->l, l, r->l); t=r;
    }
}


void split(it*t, it*&l, it*&r, int x){

    if(!t){l=r=0; return;}

    if( (t->x)<=x){
        split(t->r, t->r, r, x); l=t;
    }
    else{
        split(t->l, l, t->l, x); r=t;
    }

}




int get_max(it *t){
    if(!t){return -inf;}
    if((!t->r)){
        return t->x;
    }
    else{
        return get_max(t->r);
    }
}

int get_min(it *t){
    if(!t){return inf;}
    if(!t->l){
        return t->x;
    }
    else{
        return get_min(t->l);
    }
}


int cnt(it *t, int x){
    if(!t){return 0;}
    if(t->x==x){return t->cnt;}
    if(t->x>x){
        return cnt(t->l, x);
    }
    else{
        return cnt(t->r, x);
    }
}


void inc_cnt(it *&t, int x){
        if(!t){return ;}
        if(t->x==x){t->cnt++; return;}
    if(t->x>x){
        inc_cnt(t->l, x);
    }
    else{
        inc_cnt(t->r, x);
    }

}


void dec_cnt(it *&t, int x){
        if(!t){return ;}
        if(t->x==x){t->cnt--; return;}
    if(t->x>x){
        dec_cnt(t->l, x);
    }
    else{
        dec_cnt(t->r, x);
    }

}




int greatest_smaller(it *t, int x){
    it *n1;
    split(t, t, n1, x-1);
    int res=get_max(t);
    merge(t, t, n1);
    return res;
}

int smallest_greater(it *t, int x){
    it *n1;
    split(t, t, n1, x);
    int res=get_min(n1);
    merge(t, t, n1);
    return res;

}




void insert(it*&t, int x){
    int cn=cnt(t, x);

    if(cn>0){inc_cnt(t, x);return;}

    it *newt=new it(x, rand()), *n1;
    split(t, t, n1, x-1);
    merge(t, t, newt);
    merge(t, t, n1);
}


void erase(it *&t, int x){
    int cn=cnt(t, x);

    if(cn==0){return;}
    if(cn>1){
        dec_cnt(t, x); return;
    }

    it *n1, *n2;
    split(t, t, n1, x-1);
    split(n1, n1, n2, x);
    merge(t, t, n2);
}




int32_t main(){
INIT
srand((unsigned)(time(0)));

string s;
while(cin>>s){
    int x;
    if(s=="I"){
            cin>>x;
        int cn=cnt(R, x);
        if(cn>0){continue;}
        insert(R, x);
        int gs=greatest_smaller(R, x);
        if(gs!=(-inf) ){
            insert(D, x-gs);
        }
        int sg=smallest_greater(R, x);
        if(sg!=inf){
            insert(D, sg-x);
        }
        if((gs!=(-inf)) && (sg!=inf) ){
            erase(D, sg-gs);
        }
    }
    if(s=="S"){
            cin>>x;
        int cn=cnt(R, x);
        if(cn==0){cout<<"-1\n"; continue;}
        erase(R, x);
        int gs=greatest_smaller(R, x);
        if(gs!=(-inf) ){
            erase(D, x-gs);
        }
        int sg=smallest_greater(R, x);
        if(sg!=inf){
            erase(D, sg-x);
        }
        if((gs!=(-inf)) && (sg!=inf) ){
            insert(D, sg-gs);
        }
    }
    if(s=="C"){
            cin>>x;
        int cn=cnt(R, x);
        if(cn>0){
            cout<<"1\n";
        }
        else{
            cout<<"0\n";
        }
    }
    if(s=="MIN"){
        if(D){
            cout<<get_min(D)<<"\n";
        }
        else{
            cout<<"-1\n";
        }
    }
    if(s=="MAX"){
        int mx=get_max(R), mn=get_min(R);
        if( (mx!=mn) && (mx!=(-inf) ) && (mn!=(inf)) ){
            cout<<(mx-mn)<<"\n";
        }
        else{
            cout<<"-1\n";
        }
    }


}


return 0;
}