Cod sursa(job #1855739)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 23 ianuarie 2017 21:42:06
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

struct nod{
    int v, M, m, d, p;
    nod *l, *r; } *nil = new nod{0, (int)-2e9, (int)2e9, (int)2e9, 0, 0, 0 };

nod *mod_fiu(nod *b, const int s, nod *f){
    (s ? b->r : b->l) = f;
    b->M = max({b->v, b->l->M, b->r->M});
    b->m = min({b->v, b->l->m, b->r->m});
    b->d = min({(ll)b->v - b->l->M, (ll)b->r->m - b->v, (ll)b->l->d , (ll)b->r->d });
    return b; };

nod *join(nod *l, nod *r){
    return l == nil      ? r :
           r == nil      ? l : 
           (l->p > r->p) ? mod_fiu(l, 1, join(l->r, r)) :
                           mod_fiu(r, 0, join(l, r->l)); }

tuple<nod*, nod*> split(nod *n, const int m){
    tuple<nod*, nod*> t;
    return (n->M < m)  ? make_tuple(n, nil) :
           (n->m >= m) ? make_tuple(nil, n) :
           (n->v < m)  ? (t = split(n->r, m), get<0>(t) = mod_fiu(n, 1, get<0>(t)), t) :
                        (t = split(n->l, m), get<1>(t) = mod_fiu(n, 0, get<1>(t)), t); }

tuple<nod*, nod*, nod*> split_around(nod *n, const int m){
    auto t = split(n, m), tmp_ = split(get<1>(t), m+1);
    return make_tuple(get<0>(t), get<0>(tmp_), get<1>(tmp_)); }

nod *triple_join(nod *a, nod *b, nod*c){
    return join(join(a, b), c); }

nod *rad = nil;

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

int x;

void i_q(){
    f >> x;
    auto t = split_around(rad, x);
    rad = triple_join(get<0>(t),
        (get<1>(t) == nil ? new nod { x, x, x, (int)2e9, rand(), nil, nil } : get<1>(t)),
        get<2>(t)); }

void s_q(){
    f >> x;
    auto t = split_around(rad, x);
    if(get<1>(t) == nil) g << -1 << '\n';
    else delete get<1>(t);
    rad = join(get<0>(t), get<2>(t)); }

void c_q(){
    f >> x;
    auto t = split_around(rad, x);
    g << (get<1>(t) != nil) << '\n';
    rad = triple_join(get<0>(t), get<1>(t), get<2>(t)); }

void max_q(){
    g << (rad->M <= rad->m ? -1 : rad->M - rad->m) << '\n'; }

void min_q(){
    g << (rad->M <= rad->m ? -1 : rad->d) << '\n'; }

map<string, void (*)()> mp {
    {"I"  , &i_q },
    {"S"  , &s_q },
    {"C"  , &c_q },
    {"MAX", &max_q },
    {"MIN", &min_q } };

int main(){
    srand(time(0));
    for(string str; f >> str; ) mp[str]();
    return 0; }