Cod sursa(job #2875862)

Utilizator iulianarsenoiuArsenoiu Iulian iulianarsenoiu Data 22 martie 2022 14:49:05
Problema Zeap Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.37 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("zeap.in");
ofstream g("zeap.out");

set<int> s1;
set<int,greater<int>> s2;
multiset<int> d;

void InsertDif(int val)
{
    d.insert(val);
}

void EraseDif(int val)
{
    auto it = d.lower_bound(val);
    d.erase(it);
}

void Insert(int val)
{
    auto it = s1.lower_bound(val);
    if(it!=s1.end() && *it==val)
    {
        return;
    }
    s1.insert(val);
    s2.insert(val);
    auto dr = s1.upper_bound(val);
    auto st = s2.upper_bound(val);
    if(st!=s2.end() && dr!=s1.end())
    {
        EraseDif(*dr-*st);
    }
    if(st!=s2.end())
    {
        InsertDif(val-*st);
    }
    if(dr!=s1.end())
    {
        InsertDif(*dr-val);
    }
}

void Delete(int val)
{
    auto it = s1.lower_bound(val);
    if(it==s1.end() || *it!=val)
    {
        g<<-1<<'\n';
        return;
    }
    s1.erase(it);
    it = s2.lower_bound(val);
    s2.erase(it);
    auto dr = s1.upper_bound(val);
    auto st = s2.upper_bound(val);
    if(st!=s2.end())
    {
        EraseDif(val-*st);
    }
    if(dr!=s1.end())
    {
        EraseDif(*dr-val);
    }
    if(st!=s2.end() && dr!=s1.end())
    {
        InsertDif(*dr-*st);
    }
}

void Search(int val)
{
    auto it = s1.lower_bound(val);
    if(it==s1.end() || *it!=val)
    {
        g<<0<<'\n';
    }
    else
    {
        g<<1<<'\n';
    }
}

void MaxDif()
{
    if(s1.size()<2)
    {
        g<<-1<<'\n';
        return;
    }
    auto st = s1.begin();
    auto dr = s2.begin();
    g<<*dr-*st<<'\n';
}

void MinDif()
{
    if(s1.size()<2)
    {
        g<<-1<<'\n';
        return;
    }
    auto st = d.begin();
    g<<*st<<'\n';
}

int main()
{
    char ch;
    while(f>>ch)
    {
        if(ch=='I')
        {
            int val;
            f>>val;
            Insert(val);
        }
        else if(ch=='S')
        {
            int val;
            f>>val;
            Delete(val);
        }
        else if(ch=='C')
        {
            int val;
            f>>val;
            Search(val);
        }
        else if(ch=='M')
        {
            string t;
            f>>t;
            if(t=="AX")
            {
                MaxDif();
            }
            else
            {
                MinDif();
            }
        }
    }
    return 0;
}