Pagini recente » Cod sursa (job #1888726) | Cod sursa (job #2990099) | Cod sursa (job #2712090) | Cod sursa (job #1198101) | Cod sursa (job #462323)
Cod sursa(job #462323)
#include<fstream>
#include<algorithm>
#include<set>
//folosesc un set si un multiset
using namespace std;
int cont;
ofstream g("zeap.out");
set <int> s;
multiset < int > ms;
void sterge (int nr )
{
set <int> :: iterator it,it1,it3;
multiset <int> :: iterator it2;
it=s.find(nr);
if (it==s.end())
{
g<<"-1\n";
return ;
}
cont--;
if (it!= s.begin() )
{
it--; it1=it; it++;
it2=ms.find (*it-*it1);
ms.erase (it2);
}
it++; it1=it; it--;
if (it1!=s.end())
{
it2=ms.find(*it1-*it);
ms.erase (it2);
if (it!=s.begin())
{
it--;it3=it;it++;
ms.insert (*it1-*it3);
}
}
s.erase(it);
}
void baga (int nr )
{
set <int> :: iterator it,it1,it2;
multiset <int> :: iterator it3;
it=s.find(nr);
if (it==s.end())
{
s.insert(nr);
cont++;
it=s.find(nr);
if (it!=s.begin())
{
it--;it1=it;it++;
it++;it2=it;it--;
if (it2!=s.end())
{
it3=ms.find(*it2-*it1);
ms.erase(it3);
}
}
if (it!=s.begin())
{
it--;it1=it;it++;
ms.insert (*it-*it1);
}
it++;it1=it;it--;
if (it1!=s.end())
ms.insert (*it1-*it);
}
}
void citire ()
{
int nr;
multiset <int> :: iterator it2;
set <int > :: iterator it;
ifstream f("zeap.in");
char sir[10];
cont=0;
while (f>>sir)
{
if (strlen(sir)==1)
{
f>>nr;
if (sir[0]=='I')
baga (nr);
if (sir[0]=='S')
sterge (nr);
if (sir[0]=='C')
{
it=s.find(nr);
if (it!=s.end())
g<<"1\n";
else
g<<"0\n";
}
}
else
if (cont>=2)
{
if (sir[1]=='I')
g<<*(ms.begin())<<"\n";
else
{
it=s.end();
it--;
g<<*it-*(s.begin())<<"\n";
}
}
else
g<<"-1\n";
}
f.close();
g.close();
}
int main()
{
citire ();
return 0;
}