Cod sursa(job #1534912)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 24 noiembrie 2015 03:25:36
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.71 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

#define LE 200666
#define mp make_pair
#define x first
#define y second
#define cout g

int heap_size;
int value[LE];
bool erased[LE];

class HeapClass
{
public:
    int heap_size;
    vector<pair<int,int> > Heap;

    HeapClass()
    {
    	Heap.clear();
    	Heap.push_back(mp(0,0));
        heap_size=0;
    }

    pair<int,int> get_top()
    {
        return Heap[1];
    }

    void insert(pair<int,int> value)
    {
        Heap.push_back(value);
        ++heap_size;
        int P=heap_size;
        while (P>1&&Heap[P/2].x>Heap[P].x)
        {
        	 swap(Heap[P/2],Heap[P]);
        	 P/=2;
        }
    }

    void pop_top()
    {
      swap(Heap[1],Heap[heap_size]);
      --heap_size;
      Heap.pop_back();

      int P=1;

      while (true)
      {
         if (P*2<=heap_size&&Heap[P*2]<Heap[P])
             if (P*2+1>heap_size||Heap[P*2]<Heap[P*2+1])
             {
                swap(Heap[P*2],Heap[P]);
                P*=2;
                continue;
             }

         if (P*2+1<=heap_size&&Heap[P*2+1]<Heap[P])
         {
             swap(Heap[P*2+1],Heap[P]);
             P=P*2+1;
             continue;
         }

         break;
      }
    }
};

HeapClass H;

int get_min()
{
   while (erased[H.get_top().y])
     H.pop_top();

  return H.get_top().x;
}

int main()
{
    int nr_op,it,typ,val,nr=0;

    f>>nr_op;

    for(it=1;it<=nr_op;++it)
    {
    	f>>typ;

    	if (typ==1)
    	{
            ++nr;
    		f>>value[nr];
    		H.insert(mp(value[nr],nr));
    	}

    	if (typ==2)
    	{
    		f>>val;
    		erased[val]=true;
    	}

    	if (typ==3)
    		cout<<get_min()<<'\n';
    }

	return 0;
}