Cod sursa(job #930252)

Utilizator raulstoinStoin Raul raulstoin Data 27 martie 2013 15:30:40
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.23 kb
#include<fstream>
#include<set>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
set<int> v;
set<int>::iterator it1,it2,it3,it;
priority_queue<int,vector<int>,greater<int> > HEAP,del;
char x[20];
int n;
int minim=(1<<30),code,val;
char *p;
void decode()
{
	if(x[0]=='M')
	{
		code=4;
		return;
	}
	if(x[0]=='I')
		code=1;
	if(x[0]=='S')
		code=2;
	if(x[0]=='C')
		code=3;
	p=strchr(x,' ');
	n=atoi(p+1);
}
void DEL()
{
	while(!del.empty() && del.top()==HEAP.top())
	{
		HEAP.pop();
		del.pop();
	}
}
int main()
{
	while(fin.getline(x,20))
	{
		decode();
		if(code==1)
		{
			pair<set<int>::iterator,bool> is;
			is=v.insert(n);
			if(v.size()==2)
			{
				it1=it2=v.begin();
				HEAP.push(*(++it2)-*it1);
			}
			if(is.second && v.size()>2)
			{
				it3=is.first;
				if(it3==v.begin())
				{
					it2=it3++;
					HEAP.push(*it3-*it2);
					continue;
				}
				it2=it3++;
				it1=--it2;
				it2++;
				if(it3==v.end())
				{
					HEAP.push(*it2-*it1);
					continue;
				}
				int d1,d2;
				d1=*it2-*it1;
				d2=*it3-*it2;
				HEAP.push(min(d1,d2));
			}
			continue;
		}
		if(code==2)
		{
			if(v.empty())
			{
				fout<<-1<<'\n';
				continue;
			}
			it=v.end();
			it2=v.find(n);
			it--;
			if(it2!=v.end())
			{
				if(v.size()<2)
				{
					v.erase(it2);
					continue;
				}
				if(it2==it)
				{
					it1=--it2;
					it2++;
					del.push(*it2-*it1);
					v.erase(it2);
					continue;
				}
				it3=++it2;
				it2--;
				if(it2==v.begin())
				{
					del.push(*it3-*it2);
					v.erase(it2);
					continue;
				}
				it1=--it2;
				it2++;
				del.push(*it3-*it2);
				del.push(*it2-*it1);
				HEAP.push(*it3-*it1);
				v.erase(it2);
			}
			else
				fout<<-1<<'\n';
			continue;
		}
		if(code==3)
		{
			fout<<(v.find(n)!=v.end())<<'\n';
			continue;
		}
		if(code==4)
		{
			if(v.size()<2)
			{
				fout<<-1<<'\n';
				continue;
			}
			if(x[1]=='A')
			{
				it1=v.end();
				it1--;
				it2=v.begin();
				fout<<(*it1)-(*it2)<<'\n';
			}
			else
			{
				DEL();
				fout<<HEAP.top()<<'\n';
			}
		}
	}
	fin.close();
	fout.close();
	return 0;
}