Cod sursa(job #1402793)

Utilizator SagunistuStrimbu Alexandru Sagunistu Data 26 martie 2015 20:53:09
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <cstdio>
#include <algorithm>
#define nmax 200005

using namespace std;

struct ceva
{
    int val,ord;
};

int n,poz[nmax],nr;
ceva a[nmax];

void citire()
{
    int t,v,k;
    ceva x;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
         scanf("%d",&t);
         if(t==1)
         {
             scanf("%d",&v);
             poz[++nr]=nr;
             x.val=v;
             x.ord=nr;
             a[nr]=x;
             k=nr;
             while(k/2>=1&&a[k].val<a[k/2].val)
             {
                 swap(poz[a[k].ord],poz[a[k/2].ord]);
                 swap(a[k],a[k/2]);
                 k=k/2;
             }
         }
         else
        if(t==2)
        {
            scanf("%d",&v);
            int k=poz[v];
            while(2*k<=nr)
            {
                a[k]=a[2*k];
                poz[a[k].ord]=poz[a[2*k].ord];
                k=k*2;
            }
            nr--;
        }
        else
            printf("%d\n",a[1].val);
    }
}

int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    citire();
    return 0;
}