Cod sursa(job #1051993)

Utilizator AnonymouslegionAnonymous Anonymouslegion Data 10 decembrie 2013 19:47:55
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.85 kb
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

template<class T>
class node{
public:
  node *dad, *lt, *rt;
  T key;

  node(){};

  node(T key1){
    key = key1;
  }

  node(T key1, node* dad1){
    dad = dad1;
    key = key1;
  }
};

template<class T>
class rbst{
public:
  rbst<T>(){
    NILL.lt = NILL.rt = NILL.dad = &NILL;
  }

  void insert(T x){
    node<T> *added = new node<T>(x);
    node<T>* y = &NILL;
    node<T>* now = &root;

    while(now != &NILL){
      y = now;
      if(x < (*now).key)
        now = (*now).lt;
      else
        now = (*now).rt;
    }

    (*added).dad = y;
    if(y == &NILL)
      root.key = (*added).key;
    else if((*y).key > (*added).key)
      (*y).lt = added;
    else
      (*y).rt = added;
  }

  void erase(T x){
    erase(search(root, x));
  }

  void next(T x){
    if(x.rt != NILL)
      return min(x.rt);
    node<T> y = *(x.dad);
    while(y != NILL && x == *(y.rt)){
      x = y;
      y = *(y.dad);
    }
    return y;
  }

  void prev(T x){
    if(x.lt != NILL)
      return max(x.lt);
    node<T> y = *(x.dad);
    while(y != NILL && x = *(y.lt)){
      x = y;
      y = *(y.dad);
    }
    return y;
  }

  T min(){
    return min(root).key;
  }

  T max(){
    return max(root).key;
  }

private:
  node<T> NILL;
  node<T> root;

  node<T> min(node<T> &x){
    while(x.lt != &NILL)
      x = *(x.lt);
    return x;
  }

  node<T> max(node<T> x){
    while(*(x.rt) != NILL)
      x = *(x.lt);
    return x;
  }

  node<T>& search(node<T> &x, T key){
    if(&x == &NILL || x.key == key)
      return x;
    if(key < x.key)
      return search(*(x.lt), key);
    return search(*(x.rt), key);
  }

  void transplant(node<T> &x, node<T> &y){// instead of having dadx -- x and dady -- y we ll have dadx -- y , dadx < x and dady > y
    if(x.dad == &NILL)
      root = y;
    else if(&x == (*(x.dad)).lt)
      (*(x.dad)).lt = &y;
    else
      (*(x.dad)).rt = &y;
    if(&y != &NILL)
      y.dad = x.dad;
  }

  void erase(node<T> &x){
    if(x.lt == &NILL)
      transplant(x, *(x.rt));
    else if(x.rt == &NILL)
      transplant(x, *(x.lt));
    else{
      node<T> *y = x.rt;
      while((*y).lt != &NILL)
        y = (*y).lt;
      if((*y).dad != &x){
        transplant(*y, *(*y).rt);
        (*y).rt = x.rt;
        (*(*y).rt).dad = &x;
      }
      transplant(x, *y);
      (*y).lt = x.lt;
      (*(*y).lt).dad = y;
    }
  }
};

rbst<int> pheap;

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

  int n;
  vector<int> op;
  scanf("%d", &n);

  for(int i = 1; i <= n; ++i){
    int tip;
    scanf("%d", &tip);

    if(tip == 1){
      int x;
      scanf("%d", &x);
      op.push_back(x);
      pheap.insert(x);
    }
    else if(tip == 2){
      int x;
      scanf("%d", &x);
      pheap.erase(op[x - 1]);
    }
    else
      printf("%d\n", pheap.min());
  }

  return 0;
}