Cod sursa(job #3155039)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 7 octombrie 2023 10:37:37
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.42 kb
#include <bits/stdc++.h>

using namespace std;
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
//mt19937 rng(141297);
ifstream fin("zeap.in");
ofstream fout("zeap.out");
const int INF=2e9;
string tip;
struct node
{
    int nr,maxim,minim,mind,pri,val;
    node *l,*r;
    node(int x)
    {
        nr=1;
        l=NULL;
        r=NULL;
        maxim=x;
        minim=x;
        mind=INF;
        val=x;
        pri=rng();
    }
} *t;
int size(node *me)
{
    if(me==NULL)
        return 0;
    return me->nr;
}
void recalc(node *me)
{
    if(me==NULL)
        return;
    me->nr=1+size(me->l)+size(me->r);
    me->maxim=me->val;
    me->minim=me->val;
    me->mind=INF;
    if(me->l!=NULL)
    {
        me->maxim=max(me->maxim,me->l->maxim);
        me->minim=min(me->minim,me->l->minim);
        me->mind=min(me->mind,me->l->mind);
        me->mind=min(me->mind,me->val-me->l->maxim);
    }
    if(me->r!=NULL)
    {
        me->maxim=max(me->maxim,me->r->maxim);
        me->minim=min(me->minim,me->r->minim);
        me->mind=min(me->mind,me->r->mind);
        me->mind=min(me->mind,me->r->minim-me->val);
    }
}
array<node*,2> split(node *me, int val)
{
    if(me==NULL)
        return {NULL,NULL};
    if(me->val<=val)
    {
        node *lft=me;
        array<node*,2> b=split(me->r,val);
        lft->r=b[0];
        node *rgt=b[1];
        recalc(lft);
        recalc(rgt);
        return {lft,rgt};
    }
    else
    {
        node *rgt=me;
        array<node*,2> b=split(me->l,val);
        rgt->l=b[1];
        node *lft=b[0];
        recalc(lft);
        recalc(rgt);
        return {lft,rgt};
    }
}
node* merge(node *a, node *b)
{
    if(a==NULL)
        return b;
    if(b==NULL)
        return a;
    if(a->pri>b->pri)
    {
        node *rez=a;
        rez->r=merge(a->r,b);
        recalc(rez);
        return rez;
    }
    else
    {
        node *rez=b;
        rez->l=merge(a,b->l);
        recalc(rez);
        return rez;
    }
}
int getmax()
{
    if(size(t)<=1)
        return -1;
    return t->maxim-t->minim;
}
int getmin()
{
    if(size(t)<=1)
        return -1;
    return t->mind;
}
void putin(int x)
{
    array<node*,2> b=split(t,x);
    node *a=new node(x);
    t=merge(b[0],merge(a,b[1]));
}
void putout(int x)
{
    array<node*,2> b=split(t,x);
    array<node*,2> c=split(b[0],x-1);
    t=merge(c[0],b[1]);
}
bool have(int x)
{
    array<node*,2> b=split(t,x);
    array<node*,2> c=split(b[0],x-1);
    bool rez=(c[1]!=NULL);
    t=merge(merge(c[0],c[1]),b[1]);
    return rez;
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    t=NULL;
    while(fin>>tip)
    {
        if(tip=="MAX")
        {
            fout<<getmax()<<'\n';
            continue;
        }
        if(tip=="MIN")
        {
            fout<<getmin()<<'\n';
            continue;
        }
        int x;
        fin>>x;
        if(tip=="I")
        {
            if(!have(x))
                putin(x);
            else
                fout<<-1<<'\n';
        }
        if(tip=="S")
        {
            if(have(x))
                putout(x);
            else
                fout<<-1<<'\n';
        }
        if(tip=="C")
        {
            if(have(x))
                fout<<1<<'\n';
            else
                fout<<0<<'\n';
        }
    }
    return 0;
}