Pagini recente » Cod sursa (job #1676294) | Cod sursa (job #3039425) | Cod sursa (job #2848938) | Cod sursa (job #1741119) | Cod sursa (job #1194670)
#include <fstream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
ifstream f ("zeap.in");
ofstream g ("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()) g << h.top() << '\n';
else g << "-1\n";
}
void Zeap_difmax()
{
if(zeap.size() < 2)
{
g << "-1\n";
return;
}
set <long> :: iterator it = zeap.end();
it--;
g << -*zeap.begin() + *it << '\n';
}
int main()
{
string s;
long x;
while (f >> s)
{
if (s[0] == 'I')
{
f >> x;
Insert_Zeap(x);
}
else
if (s[0] == 'S')
{
f >> x;
if (!Delete_Zeap(x))
g << "-1\n";
}
else
if (s[0] == 'C')
{
f >> x;
g << Search_Zeap(x) << '\n';
}
else
if (s[1] == 'I')
Zeap_difmin();
else
Zeap_difmax();
}
return 0;
}