Cod sursa(job #1769987)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 3 octombrie 2016 16:47:05
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.04 kb
#include<fstream>
#include<map>
#include<string>

using namespace std;

ifstream fin ("zeap.in"); ofstream fout ("zeap.out");


const int nmax = 3 * 1e5;
const int inf = (1 << 30) - 1 + (1 << 30);
bool f[nmax + 1];
pair< int, int > v[nmax + 1];

map< int, int > mp;

string s;

void normalizare() {
    int ind = 0;
    for (map< int, int >::iterator it = mp.begin(); it != mp.end(); ++ it) {
        it -> second = ++ ind;
    }
}

struct Aint_nod{
    int mn, mx, difmin;
} aint[4 * nmax + 1];

inline int min2 (int a, int b) {
    int x = (a < b ? a : b);
    return x;
}
inline Aint_nod uneste (Aint_nod x, Aint_nod y) {
    Aint_nod ans;
    ans.mn = min2(x.mn, y.mn);
    ans.mx = -min2(-x.mx, -y.mx);
    ans.difmin = min2(x.difmin, y.difmin);
    if (x.mn != inf && y.mn != inf) {
        ans.difmin = min2(ans.difmin, y.mn - x.mx);
    }
    return ans;
}
void build (int nod, int x, int y) {
    if (x == y) {
        aint[ nod ].mn = inf; aint[ nod ].mx = -inf;
        aint[ nod ].difmin = inf;
        return ;
    }
    int mij = (x + y) / 2;
    build(2 * nod, x, mij); build(2 * nod + 1, mij + 1, y);
    aint[ nod ] = uneste(aint[2 * nod], aint[2 * nod + 1]);
}
void update (int nod, int x, int y, int pos, int val) {
    if (x == y) {
        aint[ nod ].mn = aint[ nod ].mx = val;
        if (val == inf) {
            aint[ nod ].mx = -inf;
        }
        aint[ nod ].difmin = inf;
        return ;
    }
    int mij = (x + y) / 2;
    if (pos <= mij) {
        update(2 * nod, x, mij, pos, val);
    } else {
        update(2 * nod + 1, mij + 1, y, pos, val);
    }
    aint[ nod ] = uneste(aint[2 * nod], aint[2 * nod + 1]);
}

int main() {
    int n = 0;
    while (fin >> s) {
        int x = 0, op = 0;
        if (s == "I") {
            fin >> x;
            op = 0;
        } else if (s == "S") {
            fin >> x;
            op = 1;
        } else if (s == "C") {
            fin >> x;
            op = 2;
        } else if (s == "MAX") {
            op = 3;
        } else {
            op = 4;
        }

        if (x != 0) {
            mp[ x ] = 0;
        }
        v[ ++ n ] = make_pair(op, x);
    }
    normalizare();
    build(1, 1, n);

    for (int i = 1; i <= n; ++ i) {
        int x = 0;
        if (v[ i ].second > 0) {
            x = mp[ v[ i ].second ];
        }

        if (v[ i ].first == 0) {
            if (f[ x ] == 0) {
                f[ x ] = 1;
                update(1, 1, n, x, v[ i ].second);
            }
        } else if (v[ i ].first == 1) {
            if (f[ x ] == 1) {
                f[ x ] = 0;
                update(1, 1, n, x, inf);
            } else {
                fout << "-1\n";
            }
        } else if (v[ i ].first == 2) {
            fout << f[ x ] << "\n";
        } else {
            if (aint[ 1 ].mn >= aint[ 1 ].mx) {
                fout << "-1\n";
            } else if (v[ i ].first == 3) {
                fout << aint[ 1 ].mx - aint[ 1 ].mn << "\n";
            } else {
                fout << aint[ 1 ].difmin << "\n";
            }
        }
    }

    fin.close();
    fout.close();
    return 0;
}