Cod sursa(job #528954)

Utilizator icepowdahTudor Didilescu icepowdah Data 3 februarie 2011 22:33:47
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
using namespace std;
#define MAXN 200000

int N, heap_size, insCount, poz[MAXN+1];
pair<int,int> heap[MAXN+1];
ifstream f("heapuri.in"); 
ofstream g("heapuri.out");

void min_heapify(int i) {
  int smallest, left;  
  do {
    smallest = i; left = i<<1;
    for (int j = left;j<left+2;j++) {
      if (j <= heap_size && heap[j].first < heap[smallest].first) {
        smallest = j;
      }
    }
    if (smallest == i) break;
    swap(heap[i],heap[smallest]);
    swap(poz[heap[i].second],poz[heap[smallest].second]);
    i = smallest;    
  }while (true);
}

void insert_heap(int val) { 
  int parent, i;
  insCount++;
  heap[++heap_size] = make_pair(val,insCount); 
  poz[insCount] = heap_size;
  parent = heap_size>>1;
  i = heap_size;
  while (parent > 0 && heap[parent].first > heap[i].first) {
    swap(heap[parent], heap[i]);
    swap(poz[heap[parent].second], poz[heap[i].second]);
    i = parent;
    parent = i>>1;
  }
}

int main() {
  int tip, aux, tmp;   
  f >> N;
  for (int i=1;i<=N;i++) {
    f>>tip;
    switch(tip) {
    case 1:
      f>>aux;     
      insert_heap(aux);
      break;
    case 2:
      f >> aux;
	  tmp = heap[heap_size].second;
      swap(heap[poz[aux]],heap[heap_size--]);
      poz[tmp] = poz[aux];
      min_heapify(poz[tmp]);
      break;
    case 3:
      g << heap[1].first <<"\n";
      break;
    }
  }
  f.close(); g.close();
  return 0;
}