Cod sursa(job #1148200)

Utilizator ELHoriaHoria Cretescu ELHoria Data 20 martie 2014 16:17:30
Problema Zeap Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Semestrul 2 Marime 1.78 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;
}