Cod sursa(job #1265640)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 17 noiembrie 2014 15:34:58
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 kb
#include <cstdio>
#include <algorithm>
#define Dim 200010
using namespace std;
FILE *fin=freopen("heapuri.in","r",stdin);
FILE *fout=freopen("heapuri.out","w",stdout);
int H[Dim], Pos[Dim] , L , Nr, A[Dim];

void Read()
{
    int n, op, Son, Father, x;
    scanf("%d", &n);
    for(int i = 1 ; i <= n ; ++i )
    {
        scanf("%d", &op);
        if( op == 1 )
        {
            scanf("%d", &x);
            ++L;
            ++Nr;
            H[L] = Nr;
            A[Nr] = x;
            Pos[Nr] = x;
            int aux = L;
            while( aux / 2 && A[H[aux]] < A[H[aux / 2]] )
            {
                swap( H[aux] , H[aux / 2] );
                Pos[H[x]] = x;
                Pos[H[x / 2]] = x / 2;
                x /= 2;
            }
        }

        if( op == 3 )
        {
            printf("%d\n", A[H[1]] );
        }

        if( op == 2 )
        {
            scanf("%d", &x);
            A[x] = -1;
            int aux;
            aux = Pos[x];
            while( aux / 2 && A[H[aux]] < A[H[aux / 2]] )
            {
                swap( H[aux] , H[aux/ 2] );
                Pos[H[aux]] = aux;
                Pos[H[aux / 2]] = aux / 2;
            }
            Pos[H[1]] = 0;
            H[1] = H[L--];
            Pos[H[1]] = 1;
            if( L > 1 )
            {
                aux = 1;
                int y = 0;
                while( aux != y )
                {
                    y = aux;
                    if( y * 2 <= L && A[H[aux]] > A[H[y * 2]] )
                        aux = y * 2;
                    if( y * 2 + 1 <= L && A[H[aux]] > A[H[y * 2 + 1]] )
                        aux = y * 2 + 1;
                    swap( H[aux] , H[y] );
                    Pos[H[x]] = x;
                    Pos[H[y]] = y;
                }
            }
        }

    }
}

int main()
{
    Read();
}