Cod sursa(job #1162881)

Utilizator eugen_ptrEugen Patru eugen_ptr Data 1 aprilie 2014 00:34:00
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <cstdio>

#define t (poz >> 1)
#define f1 (x << 1)
#define f2 (f1 + 1)

#define nmax 200005

using namespace std;

int H[nmax], P[nmax], V[nmax];
int top, lg;
int n;

void push(int poz)
{
    while(t && V[H[poz]] < V[H[t]])
    {
        swap(H[poz], H[t]);
        P[H[poz]] = poz;
        P[H[t]] = t;

        poz = t;
    }
}

void pop(int poz)
{
    int x;
    while(poz != x)
    {
        x = poz;
        if(f1 <= lg && V[H[poz]] > V[H[f1]])
            poz = f1;
        if(f2 <= lg && V[H[poz]] > V[H[f2]])
            poz = f2;

        swap(H[poz], H[x]);
        P[H[poz]] = poz;
        P[H[x]] = x;
    }
}

int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d",&n);
    while(n--)
    {
        int tip, x;
        scanf("%d",&tip);
        if(tip == 1)
        {
            scanf("%d",&x);
            ++top; ++lg;
            P[top] = lg;
            V[top] = x;
            H[lg] = top;

            push(lg);
        }
        else if(tip == 2)
        {
            scanf("%d",&tip);
            V[x] = -1;
            push(P[x]);

            P[H[1]] = 0;
            H[1] = H[lg--];
            P[H[1]] = 1;

            if(lg > 1)
                pop(1);
        }
        else
            printf("%d\n",V[H[1]]);
    }

    return 0;



}