Pagini recente » Cod sursa (job #1951955) | Cod sursa (job #2475901) | Cod sursa (job #1513200) | Cod sursa (job #1904436) | Cod sursa (job #2898266)
#include <iostream>
#include <fstream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
ifstream f("zeap.in");
ofstream g("zeap.out");
string st;
int x;
set<int>Z;
typedef pair<int, int> pd;
struct myComp {
constexpr bool operator()(
pair<int, int> const& a,
pair<int, int> const& b)
const noexcept
{
return abs(a.first-a.second) > abs(b.first-b.second);
}
};
priority_queue<pd, vector<pd>, myComp> zeap;
void insert(int n)
{
Z.insert(n);
set<int>::iterator itr,p,s;
itr=Z.find(n);
p=Z.find(n);
s=Z.find(n);
s++;
if(Z.size()>1) {
if (itr != Z.begin()) {
p--;
zeap.push(make_pair(*p, *itr));
}
if (s != Z.end()) {
zeap.push(make_pair(*itr, *s));
}
}
}
void erase(int n){
set<int>::iterator itr,p,s;
itr=Z.find(n);
p=Z.find(n);
s=Z.find(n);
if(itr!=Z.begin()){
p--;
}
if(itr++!=Z.end()){
s++;
}
zeap.push(make_pair(*p, *s));
Z.erase(n);
}
void max_dif(){
set<int>::iterator b,e,itr;
b=Z.begin();
e=Z.end();
e--;
g<<*e-*b<<"\n";
}
void min_dif(){
while(Z.find(zeap.top().first)==Z.end() || Z.find(zeap.top().second)==Z.end()){
zeap.pop();
}
g<<abs(zeap.top().first-zeap.top().second)<<"\n";
}
int main()
{
while(f>>st){
if(st=="I"){
f>>x;
if(Z.find(x)==Z.end()){
insert(x);
}
}
if(st=="S"){
f>>x;
if(Z.find(x)==Z.end()){
g<<-1<<"\n";
}
else{
erase(x);
}
}
if(st=="C"){
f>>x;
if(Z.find(x)==Z.end()){
g<<0<<"\n";
}
else{
g<<1<<"\n";
}
}
if(st=="MIN"){
if(Z.size()<2){
g<<-1<<"\n";
}
else{
min_dif();
}
}
if(st=="MAX"){
if(Z.size()<2){
g<<-1<<"\n";
}
else{
max_dif();
}
}
}
return 0;
}