Cod sursa(job #2897756)

Utilizator NFJJuniorIancu Ivasciuc NFJJunior Data 4 mai 2022 19:22:54
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("zeap.in");
ofstream g("zeap.out");
#define cin f
#define cout g
 
set < int > zeap;
priority_queue < int > dif, lazy;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s[0] == 'I')
		{
			int x; cin >> x;
			if(zeap.find(x) == zeap.end())
			{
				zeap.insert(x);
				auto find = zeap.find(x);
				auto before = find, after = find;
				before --, after ++;
				if(find != zeap.begin())
					dif.push(- abs(*before - *find));
				if(after != zeap.end())
					dif.push(- abs(*find - *after));
				if(find != zeap.begin() and after != zeap.end())
					lazy.push(- abs(*before - *after));
			}
		}
		else if(s[0] == 'S')
		{
			int x; cin >> x;
			if(zeap.find(x) != zeap.end())
			{
				auto find = zeap.find(x);
				auto before = find, after = find;
				before --, after ++;
				if(find != zeap.begin())
					lazy.push(- abs(*before - *find));
				if(after != zeap.end())
					lazy.push(- abs(*find - *after));
				if(find != zeap.begin() and after != zeap.end())
					dif.push(- abs(*before - *after));
				zeap.erase(x);
			}
			else
				cout<<-1<<'\n';
		}
		else if(s[0] == 'C')
		{
			int x; cin >> x;
			if(zeap.find(x) != zeap.end())
				cout<<1<<'\n';
			else
				cout<<0<<'\n';
		}
		else if(s[1] == 'A')
		{
			if(zeap.size() > 1)
				cout<<*(-- zeap.end()) - *(zeap.begin())<<'\n';
			else
				cout<<-1<<'\n';
		}
		else
		{
			if(zeap.size() > 1)
			{
				while(! lazy.empty() and dif.top() == lazy.top())
				{
					dif.pop();
					lazy.pop();
				}
				cout<<- dif.top()<<'\n';
			}
			else
				cout<<-1<<'\n';
		}
	}
	return 0;
}