Cod sursa(job #751860)

Utilizator PetcuIoanPetcu Ioan Vlad PetcuIoan Data 27 mai 2012 11:59:25
Problema Zeap Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include<stdio.h>
#include<assert.h>

#include<set>
#include<string>
#include<iostream>
#include<functional>
#include<queue>
#include<vector>
#include<algorithm>

using namespace std;

multiset<int> t;
multiset<int, greater<int> > p;
priority_queue<int, vector<int>, greater<int> > h, bad;

int zerase(int x){
  if(t.find(x) == t.end())
    return 0;

  t.erase(x);
  p.erase(x);

  if(t.count(x) >= 2){
    bad.push(0);
    return 1;
  }

  multiset<int>::iterator itlow = p.upper_bound(x), itup = t.upper_bound(x);

  bad.push(x - *itlow);
  bad.push(*itup - x);
  h.push(*itup - *itlow);

  return 1;
}

int zfind(int x){
  if(t.find(x) == t.end())
    return 0;
  return 1;
}

void zinsert(int x){
  multiset<int>::iterator itlow = p.upper_bound(x), itup = t.upper_bound(x);

  if(*itup - *itlow > 0)
    bad.push(*itup - *itlow);
  h.push(*itup - x);
  h.push(x - *itlow);
  t.insert(x);
  p.insert(x);
}

int get_min(){
  if(t.size() < 4)
    return -1;

  while(!bad.empty() && h.top() == bad.top()){
    h.pop();
    bad.pop();
  }

  return h.top();
}

int get_max(){
  if(t.size() < 4)
    return -1;

  multiset<int>::iterator itlow = t.upper_bound(-9001), ithigh = p.lower_bound(1000000001);
  return *ithigh - *itlow;
}

int main(){
  assert(freopen("zeap.in", "r", stdin));
  assert(freopen("zeap.out", "w", stdout));

  string qry;

  t.insert(-1000000005);
  t.insert(2000000005);
  p.insert(-1000000005);
  p.insert(2000000005);

  while(cin >> qry){
    if(qry[0] == 'M')
      if(qry[1] == 'I')
        printf("%d\n", get_min());
      else
        printf("%d\n", get_max());
    else if(qry[0] == 'I'){
      int aux;
      scanf("%d", &aux);

      zinsert(aux);
    }
    else if(qry[0] == 'S'){
      int aux;
      scanf("%d", &aux);

      if(!zerase(aux))
        printf("-1\n");
    }
    else{
      int aux;
      scanf("%d", &aux);

      printf("%d\n", zfind(aux));
    }

    scanf("\n");
  }

  return 0;
}