Pagini recente » Cod sursa (job #1731653) | Cod sursa (job #3210176) | Monitorul de evaluare | Cod sursa (job #1291653) | Cod sursa (job #1194414)
#include <fstream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
set <long> zeap;
priority_queue <long, vector<long>, greater<long> > h, p;
void Insert_Heap(long val)
{
h.push(val);
}
void Delete_Heap(long x)
{
p.push(x);
}
void Insert_Zeap(long x)
{
set <long> :: iterator it;
if(zeap.find(x) != zeap.end()) return;
it = zeap.lower_bound(x);
if(it != zeap.begin()) it--;
int st = *it;
it = zeap.upper_bound(x);
int dr = *it;
if(st && st < x)
Insert_Heap(x - st);
if(dr && dr > x)
Insert_Heap(dr - x);
if(st < x && x < dr)
Delete_Heap(dr - st);
zeap.insert(x);
}
bool Delete_Zeap(int x)
{
set <long> :: iterator it, itt;
itt = zeap.find(x);
if(itt == zeap.end()) return 0;
zeap.erase(itt);
it = zeap.lower_bound(x);
if(it != zeap.begin()) it--;
int st = *it;
it = zeap.upper_bound(x);
int dr = *it;
if(st && st < x)
Delete_Heap(x - st);
if(dr && dr > x)
Delete_Heap(dr - x);
if(st < x && x < dr)
Insert_Heap(dr - st);
return 1;
}
bool Search_Zeap(long x)
{
return zeap.find(x) != zeap.end();
}
void Zeap_difmin()
{
while(h.size() && p.size() && h.top() == p.top())
{
h.pop();
p.pop();
}
if(h.size()) fout<<h.top()<<'\n';
else fout<<"-1\n";
}
void Zeap_difmax()
{
if(zeap.size() < 2)
{
fout<<"-1\n";
return;
}
set <long> :: iterator it = zeap.end(); it--;
fout<<-*zeap.begin() + *it<<'\n';
}
int main()
{
string s; long x;
while(fin>>s)
{
if(s[0] == 'I')
{
fin>>x;
Insert_Zeap(x);
}else
if(s[0] == 'S')
{
fin>>x;
if(!Delete_Zeap(x))
fout<<"-1\n";
}else
if(s[0] == 'C')
{
fin>>x;
fout<<Search_Zeap(x)<<'\n';
}else
if(s[1] == 'I')
Zeap_difmin();
else
Zeap_difmax();
}
return 0;
}