Cod sursa(job #2563205)

Utilizator memecoinMeme Coin memecoin Data 1 martie 2020 07:13:59
Problema Zeap Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.35 kb
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3f

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "zeap";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

set<int> s;
multiset<int> dif;

void maxV() {
    if (s.size() < 2) {
        fout << -1 << "\n";
        return;
    }
    
    fout << *(--s.end()) - *s.begin() << "\n";
}

void minV() {
    if (s.size() < 2) {
        fout << -1 << "\n";
        return;
    }
    
    fout << *(dif.begin()) << "\n";
}

void insert(int x) {
    if (s.count(x)) {
        return;
    }
    
    s.insert(x);
    
    auto it = s.find(x);
    
    int prev = 0;
    int nxt = 0;
    
    if (it != s.begin()) {
        it--;
        prev = *it;
        it++;
    }
    
    if (it != (--s.end())) {
        it++;
        nxt = *it;
    }

    if (prev != 0 && nxt != 0) {
        dif.erase(nxt - prev);
    }
    
    if (prev != 0) {
        dif.insert(x - prev);
    }
    
    if (nxt != 0) {
        dif.insert(nxt - x);
    }
}

void remove(int x) {
    if (s.count(x) == 0) {
        fout << -1 << "\n";
        return;
    }
    
    auto it = s.find(x);
    
    int prev = 0;
    int nxt = 0;
    
    if (it != s.begin()) {
        it--;
        prev = *it;
        it++;
    }
    
    if (it != (--s.end())) {
        it++;
        nxt = *it;
    }
    
    if (prev != 0 && nxt != 0) {
        dif.insert(nxt - prev);
    }
    
    if (prev != 0) {
        dif.erase(x - prev);
    }
    
    if (nxt != 0) {
        dif.erase(nxt - x);
    }
    
    s.erase(x);
}

void search(int x) {
    fout << (s.count(x) > 0) << "\n";
}

int main() {
    
    while (true) {
        string op;
        fin >> op;
        
        if (fin.eof()) {
            break;
        }
        
        if (op == "MAX") {
            maxV();
            continue;
        }
        
        if (op == "MIN") {
            minV();
            continue;
        }
        
        int x;
        fin >> x;
        
        if (op == "I") {
            insert(x);
        }
        
        if (op == "S") {
            remove(x);
        }
        
        if (op == "C") {
            search(x);
        }
    }
    
    
    return 0;
}