Cod sursa(job #1202496)

Utilizator DenisacheDenis Ehorovici Denisache Data 28 iunie 2014 02:13:46
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <cstdio>
#include <algorithm>
using namespace std;
int N,op,x;
struct heap
{
    int Pos[200005],A[200005],H[200005],NR,L;
    inline void Push(int x)
    {
        while (x>>1 && A[H[x]]<A[H[x>>1]])
        {
            Pos[H[x]]=x>>1; Pos[H[x>>1]]=x;
            swap(H[x],H[x>>1]);
            //Pos[H[x]]=x; Pos[H[x>>1]]=x>>1;
            x>>=1;
        }
    }
    inline void Pop(int x)
    {
        int y=0;
        while (x!=y)
        {
            y=x;
            if (y<<1<=L && A[H[x]]>A[H[y<<1]]) x=y<<1;
            if ((y<<1)+1<=L && A[H[x]]>A[H[(y<<1)+1]]) x=(y<<1)+1;
            swap(H[x],H[y]);
            Pos[H[x]]=x;
            Pos[H[y]]=y;
        }
    }
    void Insert(int x)
    {
        A[++NR]=x;
        H[++L]=NR;
        Pos[NR]=L;
        Push(L);
    }
    void Erase(int x)
    {
        A[x]=-1;
        Push(Pos[x]);
        Pos[H[1]]=0;
        H[1]=H[L--];
        Pos[H[1]]=1;
        if (L>1) Pop(1);
    }
    void Min() {printf("%d\n",A[H[1]]);}
};
heap V;
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d",&N);
    for (register int i=1;i<=N;i++)
    {
        scanf("%d",&op);
        if (op==1)
        {
            scanf("%d",&x);
            V.Insert(x);
        }
        else if (op==2)
        {
            scanf("%d",&x);
            V.Erase(x);
        }
        else V.Min();
    }
    return 0;
}