Cod sursa(job #1155303)

Utilizator otto1Palaga Vicentiu-Octavian otto1 Data 26 martie 2014 20:13:25
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.47 kb
#include <fstream>
#include <algorithm>
#include <set>
#include <map>
#include <string>

using namespace std;

int main()
{
    ifstream cin("zeap.in");
    ofstream cout("zeap.out");
    set<int> s;
    map<int, int> d;
    string instruction;

    auto eraseDif = [&](const int& x) {
        auto it = d.find(x);
        it->second--;
        if (!it->second) {
            d.erase(it);
        }
    };

    while (getline(cin, instruction)) {
        int x;
        if (instruction[0] == 'I') {
            sscanf(instruction.c_str(), "I %d", &x);
            auto it = s.insert(x);
            if (it.second == true) {
                if (s.size() == 1) continue;
                set<int>::iterator p = it.first;
                set<int>::iterator it1 = p;
                set<int>::iterator it2 = p;
                if (p == s.begin()) {
                    it2++;
                    d[*it2 - *p]++;
                } else
                if (p == --s.end()) {
                    it1--;
                    d[*p - *it1]++;
                } else {
                    it1--;
                    it2++;
                    d[*p - *it1]++;
                    d[*it2 - *p]++;
                }
            }

        } else
        if (instruction[0] == 'S') {
            sscanf(instruction.c_str(), "S %d", &x);
            set<int>::iterator p = s.find(x);
            if (p == s.end()) {
                cout << "-1\n";
            } else {
                set<int>::iterator it1 = p;
                set<int>::iterator it2 = p;
                int num = 0;
                if (p != s.begin()) {
                    it1--;
                    eraseDif(*p - *it1);
                    num++;
                }

                if (p != --s.end()) {
                    it2++;
                    eraseDif(*it2 - *p);
                    num++;
                }

                if (num == 2) {
                    d[*it2 - *it1]++;
                }

                s.erase(p);
            }
        } else
        if (instruction[0] == 'C') {
            sscanf(instruction.c_str(), "C %d", &x);
            cout << (s.find(x) != s.end()) << "\n";
        } else
        if (instruction[1] == 'A') {
            cout << (s.size() < 2 ? -1 : *s.rbegin() - *s.begin()) << "\n";
        } else
        if (instruction[1] == 'I') {
            cout << (d.empty() ? - 1 : d.begin()->first) << "\n";
        }
    }
    return 0;
}