Cod sursa(job #775571)

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

using namespace std;

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

int main()
{
	ifstream fin("zeap.in");
	freopen("zeap.out", "w", stdout);

	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()) printf("-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()) printf("0\n");
			else               printf("1\n");
		}
		else
		{
			fin >> ch1 >> ch2;
			if (ch1 == 'A')
			{
				if (S.size() >= 2)
				{
					set<int>::iterator it = S.end();
					--it;
					
					printf("%d\n", *it - *S.begin());
				}
				else printf("-1\n");
			}
			else
			{
				if (S.size() >= 2) printf("%d\n", *D.begin());
				else               printf("-1\n");        
			}
		}
	}
	
	fin.close();
}