Pagini recente » Cod sursa (job #1437319) | Cod sursa (job #2635724) | Cod sursa (job #379152) | Cod sursa (job #1811878) | Cod sursa (job #2760965)
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
set<long long>zp; //in acest set retin in ordine crescatoare toate valorile din zeap. Astfel pot rezolva toate operatiile mai putin cea de diferenta minima
bool v[1000000000]={0};
struct diferenta{
long long x,y;//perechi de forma predecesor valoare sau valoare succesor
long long mini;
};
struct pereche{
long long prim,sec;
};
pereche vecini(set<long long> s,long long x)
{
pereche aux;
aux.prim = -1;
aux.sec = -1;
set<long long>::iterator it = s.find(x);
if(it!=s.begin())
{
it--;
aux.prim = *it;
it++;
}
it++;
if(it!=s.end())
{
aux.sec = *it;
}
return aux;
}
struct compare_mini{
bool operator()(diferenta const& d1, diferenta const& d2)
{
return d1.mini > d2.mini;
}
};
priority_queue<diferenta,vector<diferenta>,compare_mini> pq;
int main()
{
string com;
while(fin>>com)
{
if(com == "I")
{
int x;
fin>>x;
if(v[x]==0)
{
zp.insert(x);
v[x] = 1;
pereche aux2 = vecini(zp,x);
diferenta aux3,aux4;
if(aux2.prim!=-1)
{
aux3.x = aux2.prim;
aux3.y = x;
aux3.mini = x- aux2.prim;
pq.push(aux3);
}
if(aux2.sec != -1)
{
aux4.x = x;
aux4.y = aux2.sec;
aux4.mini = aux2.sec - x;
pq.push(aux4);
}
}
}
if(com == "S")
{
int x;
fin>>x;
if(v[x] == 0)
fout<<-1<<"\n";
else
{
if(vecini(zp,x).prim!=-1 && vecini(zp,x).sec!=-1)
{
diferenta aux5;
aux5.x = vecini(zp,x).prim;
aux5.y = vecini(zp,x).sec;
aux5.mini = aux5.y - aux5.x;
pq.push(aux5);
}
v[x] = 0;
zp.erase(x);
}
}
if(com == "C")
{
int x;
fin>>x;
if(v[x]==0)
fout<<0<<"\n";
else
fout<<1<<"\n";
}
if(com == "MAX")
{
if(zp.size()<2)
fout<<-1<<"\n";
else
{
set<long long>::iterator it = zp.begin();
set<long long>::iterator it2 = zp.end();
it2--;
fout<<abs(*it - *it2)<<"\n";
}
}
if(com == "MIN")
{
if(zp.size()<2)
fout<<-1<<"\n";
else
{
while(!pq.empty() && (v[pq.top().x] == 0|| v[pq.top().y]== 0))
pq.pop();
fout<<pq.top().mini<<"\n";
}
}
}
return 0;
}