Cod sursa(job #775569)

Utilizator darrenRares Buhai darren Data 8 august 2012 15:26:45
Problema Zeap Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <fstream>
#include <set>

using namespace std;

set<int> S;
multiset<int> D;

int main()
{
	ifstream fin("zeap.in");
	ofstream fout("zeap.out");

	char opt;
	while (fin >> opt)
	{
		char ch1, ch2;
		int num;
		
		if (opt == 'I')
		{
			fin >> num;
			
			set<int>::iterator it = S.find(num);
			if (it == S.end())
			{
				S.insert(num);
				
				set<int>::iterator itn = S.upper_bound(num);
				if (itn != S.end())
				{
					set<int>::iterator aux = itn;
					--aux;
					
					multiset<int>::iterator mnow = D.find(*itn - num);
					D.insert(*itn - num);
					
					if (aux == S.begin()) continue;
					--aux;
					mnow = D.find(*itn - *aux);
					D.erase(mnow);
				}
				--itn;
				if (itn == S.begin()) continue;
				--itn;
				D.insert(num - *itn);
			}
		}
		else if (opt == 'S')
		{
			fin >> num;
			set<int>::iterator it = S.find(num);
			if (it == S.end()) fout << -1 << '\n';
			else
			{
				S.erase(it);
				if (S.size() == 0) continue;
				
				set<int>::iterator itn = S.upper_bound(num);
				if (itn != S.end())
				{
					multiset<int>::iterator mnow = D.find(*itn - num);
					D.erase(mnow);
				}
				if (itn != S.begin())
				{
					--itn;
					multiset<int>::iterator mnow = D.find(num - *itn);
					D.erase(mnow);
					
					int auxv = *itn;
					++itn;
					if (itn != S.end())
						D.insert(*itn - auxv);
				}
			}
		}
		else if (opt == 'C')
		{
			fin >> num;
			set<int>::iterator it = S.find(num);
			if (it == S.end()) fout << 0 << '\n';
			else               fout << 1 << '\n';
		}
		else
		{
			fin >> ch1 >> ch2;
			if (ch1 == 'A')
			{
				if (S.size() >= 2)
				{
					set<int>::iterator it = S.end();
					--it;
					
					fout << *it - *S.begin() << '\n';
				}
				else fout << -1 << '\n';
			}
			else
			{
				if (S.size() >= 2) fout << *D.begin() << '\n';
				else               fout << -1 << '\n';          
			}
		}
	}
	
	fin.close();
	fout.close();
}