Cod sursa(job #1148891)

Utilizator dr_personalityEftime Andrei Horatiu dr_personality Data 21 martie 2014 11:25:32
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <fstream>
using namespace std;
ifstream in( "heapuri.in" );
ofstream out( "heapuri.out" );
 
const int nmax = 200005;
int n, k, nr, nr1, x, operatie;
int heap[nmax], poz[nmax], v[nmax];
int aux;
 
void jos(int shp, int n)
{
    int st = shp*2;
    if ( st<=n )
    {
        if (st+1<=n && v[heap[st+1]] < v[heap[st]])
        {
			 ++st;
        }
        if (v[heap[st]]<v[heap[shp]])
        {			
			aux = heap[st];
			heap[st] = heap[shp];
			heap[shp] = aux;
			poz[heap[shp]] = shp;
			poz[heap[st]] = st;

            jos(st,n);
        }
    }
}
 
void update_heap(int shp)
{
    int tata = shp/2;
    if (tata>=1)
    {
        if ( v[heap[tata]]>v[heap[shp]] )
        {
			aux = heap[tata];
			heap[tata] = heap[shp];
			heap[shp] = aux;
			poz[heap[shp]]= shp;
			poz[heap[tata]]= tata;

            update_heap(tata);
        }
    }
}

int main()
{
	int player_unu=0;

    in >> n;
    for( int i= 1; i<=n; ++i )
    {
        in >> operatie;

        if(operatie==1)
		{
			in>>x;

			v[++nr1]= x;
			poz[nr1]= nr;
			heap[++nr]= nr1;

			update_heap(nr);
		}

        if(operatie==2) 
		{
			int y;
			in >> x;
			y=poz[x];


			aux = heap[poz[x]];
			heap[poz[x]] = heap[nr];
			heap[nr] = aux;
			poz[heap[nr]] = nr;
			poz[heap[poz[x]]] = poz[x];

			nr--;
			jos(y,nr);
		}

        if(operatie==3)
		{
			out<<v[heap[1]]<<'\n';
		}
    }

    return player_unu;
}