Cod sursa(job #1341983)

Utilizator badea_adi1999Badea Adrian Catalin badea_adi1999 Data 13 februarie 2015 13:01:43
Problema Zeap Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <fstream>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#define nmax 300005
#define inf 1<<30
using namespace std;
ifstream f("zeap.in");
ofstream g("zeap.out");
set <int> se;
map <int , int > mapi;

int n=300000,n1,m,poz;
char s[20];
struct element{
    bool ok;
    int mi,ma;
    int difmin,difmax;
    ;};
int a[nmax];
element v[nmax*5];


int modul(int x)
{
    if (x<0) return -x;
    return x;
}
void update(int st, int dr, int nod)
{
    if (st==dr) {
        v[nod].ok=true;
        if (a[st]==inf) v[nod].ok=false;
        v[nod].mi=a[st];
        v[nod].ma=a[st];
        v[nod].difmin=inf;
        v[nod].difmax=-inf;
        return;
    }
    int mid=(st+dr)>>1,x;


    if (poz<=mid) update(st, mid , nod * 2 );
    if (poz>mid) update(mid+1 , dr , nod *2 +1 );

    if (v[nod*2].ok==true||v[nod*2+1].ok==true)
            v[nod].ok=true;
        else
            v[nod].ok=false;

    v[nod].mi=inf;
    v[nod].ma=-inf;

    if (v[nod * 2].ok==true)
            v[nod].mi=min(v[nod].mi, v[ nod *2] . mi);
    if (v[nod * 2+1].ok==true)
            v[nod].mi=min(v[nod].mi, v[nod * 2 +1].mi);

    if (v[nod * 2].ok==true)
            v[nod].ma=max(v[nod].ma, v[nod * 2].ma);

    if (v[nod * 2+1].ok==true)
            v[nod].ma=max(v[nod].ma, v[nod * 2 +1].ma);



    v[nod].difmax=v[nod].ma-v[nod].mi;

    x=1<<30;

    if (v[nod *2].ok==true)
        x=min(x,v[nod * 2].difmin);
    if (v[nod *2+1].ok==true)
        x=min(x,v[nod *2 +1].difmin);

    if (v[nod *2].ok==true&&v[nod * 2+1].ok==true) {
            x=min(x,modul(v[nod *  2].ma-v[nod * 2 + 1].ma));
            x=min(x,modul(v[nod *  2].mi-v[nod * 2 + 1].mi));
    }

    v[nod].difmin=x;

}


void solve()
{
     if (strcmp(s,"MIN")==0)
            g<<v[1].difmin<<'\n';
     if (strcmp(s,"MAX")==0)
            g<<v[1].difmax<<'\n';
     int x=0;
     for (int i=2;i<m;i++) x=x*10+s[i]-'0';

     if (s[0]=='C') {
            g<<se.count(x)<<'\n';
            return;
     }
     if (s[0]=='I') {
            n1++;
            a[n1]=x;
            se.insert(x);
            mapi[x]=n1;
            poz=n1;
            update(1,n,1);
            return;
     }
     if (s[0]=='S') {
            if (se.count(x)==0) {
                g<<-1<<'\n';
                return;
            }
            poz=mapi[x];
            a[poz]=inf;
            update(1,n,1);
     }
}
int main()
{
    int i,j;
    while (1) {
        f.getline(s,20);
        m=strlen(s);
        if (m==0)
            break;
        solve();

    }
    return 0;
}