Cod sursa(job #2563209)

Utilizator memecoinMeme Coin memecoin Data 1 martie 2020 07:31:54
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.13 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;
map<int, 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()->first << "\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[nxt - prev]--;
        if (dif[nxt - prev] == 0) {
            dif.erase(nxt - prev);
        }
    }
    
    if (prev != 0) {
        dif[x - prev]++;
    }
    
    if (nxt != 0) {
        dif[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[nxt - prev]++;
    }
    
    if (prev != 0) {
        dif[x - prev]--;
        if (dif[x - prev] == 0) {
            dif.erase(x - prev);
        }
    }
    
    if (nxt != 0) {
        dif[nxt - x]--;
        if (dif[nxt - x] == 0) {
            dif.erase(nxt - x);
        }
    }
    
    s.erase(x);
}

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

int k = 0;

int nextNumber(string &s) {
    while (s[k] == ' ' || s[k] == '\n') {
        k++;
    }
    
    int rez = 0;
    
    while (s[k] >= '0' && s[k] <= '9') {
        rez *= 10;
        rez += s[k] - '0';
        k++;
    }
    
    return rez;
}

string nextToken(string &s) {
    while (s[k] == ' ' || s[k] == '\n') {
        k++;
    }
    
    string token = "";
    
    while (s[k] != ' ' && s[k] != '\n' && s[k] != 0) {
        token += s[k];
        k++;
    }
    
    return token;
}

int main() {
    
    string s((istreambuf_iterator<char>(fin)), (istreambuf_iterator<char>()));
    
    while (true) {
        string op = nextToken(s);
        
        if (s[k] == 0) {
            break;
        }
        
        if (op == "MAX") {
            maxV();
            continue;
        }
        
        if (op == "MIN") {
            minV();
            continue;
        }
        
        int x = nextNumber(s);
        
        if (op == "I") {
            insert(x);
        }
        
        if (op == "S") {
            remove(x);
        }
        
        if (op == "C") {
            search(x);
        }
    }
    
    
    return 0;
}