Cod sursa(job #1260294)

Utilizator angelaAngela Visuian angela Data 11 noiembrie 2014 07:30:45
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
// 100 p (infoarena)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define NMax 200001

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int D[NMax];
int p, cod, x, m;

class Heap {
public:
	Heap(int n = 0) : nH(0), H(vector<int>(n + 1)), P(vector<int>(n + 1))
	{}
	void pop(int x) 
	{
		x = P[x];
		Swap(x, nH--);
		int p = 1, s = 2;
		while ( s <= nH ) 
		{
			if ( s + 1 <= nH && H[s + 1] < H[s] )
				s++;
			if ( H[s] < H[p] )
			{
				Swap(s, p);	
				p = s;	
				s = 2 * p;
			}
			else break;
		}
	}
	
	void push(int x) 
	{
		H[++nH] = x; P[x] = nH;
		up(x);
	}
	
	void up(int x) 
	{
		int s = P[x], p = s / 2;
		while ( p != 0 && H[s] < H[p] ) 
		{
			Swap(p, s);	
			s = p; p = s / 2;
		}
	}
	
	bool empty() const
	{
		return nH <= 0;
	}
	int top() const
	{
		return H[1];
	}
private:	
	void Swap(int s, int p) 
	{
		swap(H[s], H[p]);
		P[H[p]] = p;
		P[H[s]] = s;
	}
	int nH;
	vector<int> H, P;
};

int main()
{
    fin >> m;
    Heap heap(m + 1);
    for (int i = 1; i <= m; ++i)
    {
        fin >> cod;
        if ( cod == 3 )
        {
            fout << heap.top() << '\n';
            continue;
        }
        fin >> x;
        if (cod == 1)
        {
            D[++p] = x;
            heap.push(x);
		}

        if ( cod == 2 )
			heap.pop(D[x]);

    }
    return 0;
}