Cod sursa(job #2474481)

Utilizator Coroian_DavidCoroian David Coroian_David Data 15 octombrie 2019 11:58:51
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.69 kb
#include <bits/stdc++.h>

using namespace std;

/**reads too much
int DONE = 0;

FILE *f;

int crChar = 4096;

const int buffSize = 4096;

char buff[4100];

bool isDigit[300];

void getIsDigit()
{
    int i;
    for(i = '0'; i <= '9'; i ++)
        isDigit[i] = 1;
}

void refillBuff()
{
    crChar = 0;

    if(feof(f))
        DONE = 1;

    else
        fread(buff, 1, buffSize, f);
}

char getCr()
{
    if(crChar == buffSize)
        refillBuff();

    return buff[crChar ++];
}

int getNr()
{
    int nr = 0;
    int sign = 1;

    char c = getCr();

    while(isDigit[c] == 0 && c != '-')
        c = getCr();

    while(c == '-')
    {
        sign *= -1;

        c = getCr();
    }

    while(isDigit[c] == 1)
    {
        nr = nr * 10 + c - '0';

        c = getCr();
    }

    return nr * sign;
}

bool isChar(char c)
{
    return (c >= 'A' && c <= 'Z');
}

string tmp;

string getSir()
{
    tmp.clear();

    char c = getCr();
    while(isChar(c) == 0)
        c = getCr();

    while(isChar(c) == 1)
    {
        tmp.resize(tmp.size() + 1);
        tmp[tmp.size() - 1] = c;

        c = getCr();
    }

    return tmp;
}
**/
const int sqrInf = (1e9) - 1;

struct nod
{
    int mn, mx;
    int dn;
    int pr, nr;
    nod *fst = NULL, *fdr = NULL;

    nod(int _mn, int _mx, int _dn, int _pr, int _nr) : mn(_mn), mx(_mx), dn(_dn), pr(_pr), nr(_nr){}

    nod(){}
};

nod *nil;
nod *rad;

int Q;

bool p(int nr)
{
    return (Q > nr);
}

nod *mfiu(nod *n, int care, nod *fiu){
    (care == 0 ? n->fst : n->fdr) = fiu;
    n->mx = max({n->fst->mx, n->fdr->mx, n->nr});
    n->mn = min({n->fst->mn, n->fdr->mn, n->nr});
    n->dn = min({n->fst->dn, n->fdr->dn, n->fdr->mn - n->nr, n->nr - n->fst->mx });
    return n;
}

pair <nod*, nod*> split(nod *n)
{
    if(n == nil)
        return {nil, nil};

    if(p(n -> nr))
    {
        auto tmp = split(n -> fdr);
        mfiu(n, 1, tmp.first);
        return {n, tmp.second};
    }

    else
    {
        auto tmp = split(n -> fst);
        mfiu(n, 0, tmp.second);
        return {tmp.first, n};
    }
}

nod *join(nod *st, nod *dr)
{
    if(st == nil)
        return dr;

    if(dr == nil)
        return st;

    if((st -> pr) > (dr -> pr))
        return mfiu(st, 1, join(st -> fdr, dr));

    else
        return mfiu(dr, 0, join(st, dr -> fst));
}

char x[20];

bool isDigit[300];

void getIsDigit()
{
    int i;
    for(i = '0'; i <= '9'; i ++)
        isDigit[i] = 1;
}

int getNr()
{
    int n = strlen(x);

    int crChar = 0;
    while(isDigit[x[crChar]] == 0)
        crChar ++;

    int nr = 0;
    while(crChar < n && isDigit[x[crChar]] == 1)
    {
        nr = nr * 10 + x[crChar] - '0';

        crChar ++;
    }

    return nr;
}

void ansQues()
{
    getIsDigit();

    FILE *f = fopen("zeap.in", "r");

    rad = nil;
    int cnt = 0;

    srand(time(0));

    ofstream g("zeap.out");

    while(fgets(x, 19, f))
    {
        int sz = strlen(x);

        if(sz < 1)
            break;

        if(!(x[0] >= 'A' && x[0] <= 'Z'))
            break;

        if(x[0] != 'M')
        {
            int val;
           // cout<< val << "\n";
            val = getNr();

            nod *nou;
            if(x[0] == 'I')
            {
                nou = new nod;
                nou -> pr = rand();
                nou -> mx = nou -> mn = nou -> nr = val;
                nou -> fst = nou -> fdr = nil;
                nou -> dn = sqrInf;
            }

            Q = val;
            auto tmp = split(rad);

            Q = val + 1;
            auto tmp2 = split(tmp.second);

            if(x[0] == 'I')
            {
                if(tmp2.first == nil)
                    tmp2.first = nou, cnt ++;
            }

            if(x[0] == 'C')
                g << (tmp2.first == nil ? 0 : 1) << "\n";

            if(x[0] == 'S')
            {
                if(tmp2.first == nil)
                    g << "-1\n";

                else
                {
                    delete tmp2.first;
                    tmp2.first = nil;
                    cnt --;
                }
            }

            rad = join(tmp.first, join(tmp2.first, tmp2.second));
        }

        else
        {
            if(x[1] == 'A')
                g << (cnt > 1 ? rad -> mx - rad -> mn : -1) << "\n";

            if(x[1] == 'I')
                g << (cnt > 1 ? rad -> dn : -1) << "\n";
        }
    }

    fclose(f);
    g.close();
}

int main()
{
    nil = new (nod) { (int)1e9, (int)-1e9, (int)1e9, 0, 0 };

    ansQues();

    return 0;
}