Cod sursa(job #2386030)

Utilizator horiacoolNedelcu Horia Alexandru horiacool Data 22 martie 2019 02:11:52
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.75 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
 
int N,h,k,heap[200001],val[200001],poz[200001];
 
inline int father(int nod)
{
    return nod / 2;
}
inline int left_son(int nod)
{
    return nod * 2;
}
inline int right_son(int nod)
{
    return nod * 2 + 1;
}
 
void up(int nod)
{
     int aux1,aux2,ok = 0;
 
     while( nod > 1 && ok == 0 )
     {
        if( val[ heap[ nod ] ] >= val[ heap[ father(nod) ] ] )
            ok = 1;
        else
        {
            aux1 = heap[ father(nod) ];
            aux2 = heap[ nod ];
 
            heap[ nod ] = aux1;
            heap[ father(nod) ] = aux2;
 
            poz[ aux1 ] = nod;
            poz[ aux2 ] = father(nod);
 
            nod = father(nod);
        }
     }
}
 
void down(int nod)
{
    int aux1,aux2,ok = 0;
 
    while( nod * 2 <= h && ok == 0 )
    {
        if( val[ heap[ nod ] ] <= val[ heap[ left_son(nod) ] ] && val[ heap[ nod ] ] <= val[ heap[ right_son(nod) ] ] )
            ok = 1;
        else
        if( val[ heap[ nod ] ] <= val[ heap[ left_son(nod) ] ] && right_son(nod) > h  )
            ok = 1;
        else
        {
            if( val[ heap[ nod ] ] > val[ heap[ left_son(nod) ] ] && ( val[ heap[ left_son(nod) ] ] <= val[ heap[ right_son(nod) ] ] || right_son(nod) > h ) )
            {
                aux1 = heap[ left_son(nod) ];
                aux2 = heap[ nod ];
 
                heap[ nod ] = aux1;
                heap[ left_son(nod) ] = aux2;
 
                poz[ aux1 ] = nod;
                poz[ aux2 ] = left_son(nod);
 
                nod = left_son(nod);
            }
            else
            if( val[ heap[ nod ] ] > val[ heap[ right_son(nod) ] ] && val[ heap[ left_son(nod) ] ] >= val[ heap[ right_son(nod) ] ] )
            {
                aux1 = heap[ right_son(nod) ];
                aux2 = heap[ nod ];
 
                heap[ nod ] = aux1;
                heap[ right_son(nod) ] = aux2;
 
                poz[ aux1 ] = nod;
                poz[ aux2 ] = right_son(nod);
 
                nod = right_son(nod);
            }
        }
    }
}
 
void Eliminare(int nod)
{
    int aux = heap[h];
    poz[ aux ] = poz[ nod ];
    heap[ poz[ nod ] ] = aux;
    poz[ nod ] = 0;
    heap[h] = 0;
    h--;
 
    up( poz[ aux ] );
    down( poz[ aux ] );
}
 
int main()
{
    f>>N;
 
    int a,b;
 
    for(int i = 1 ; i <=N ; i++)
    {
        f>>a;
 
        if( a == 3 )
           g<<val[ heap[1] ]<<'\n';
        else
        if( a == 2 )
        {
            f>>b;
            Eliminare(b);
        }
        else
        if( a == 1 )
        {
            f>>b;
            k++;
            h++;
            val[k] = b;
            poz[k] = h;
            heap[h] = k;
            up(h);
        }
    }
 
    return 0;
}