Cod sursa(job #1043447)

Utilizator vlady1997Vlad Bucur vlady1997 Data 28 noiembrie 2013 16:58:45
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
        #include <cstdio>
        using namespace std;
        int a[200001], h[200001], poz[200001], m=0, n=0;
        void Swap (int i, int j)
        {
            int aux;
            aux=h[i]; h[i]=h[j]; h[j]=aux;
            aux=poz[h[i]]; poz[h[i]]=poz[h[j]]; poz[h[j]]=aux;
        }
        void push (int i)
        {
            bool ok=false;
            while (ok==false)
            {
                if (a[i]<a[i/2]) {Swap(i,i/2); i=i/2;}
                else ok=true;
            }
        }
        void del (int p)
        {
            Swap(p,n);
            push(p);
        }
        int main()
        {
            int i, t, x, z, p;
            freopen("heapuri.in","r",stdin);
            freopen("heapuri.out","w",stdout);
            scanf("%d",&t);
            for (i=1; i<=t; i++)
            {
                scanf("%d",&z);
                if (z==1)
                {
                    scanf("%d",&x); a[++n]=x; h[n]=n; poz[n]=n; m++; push(n);
                }
                else if (z==2)
                {
                    scanf("%d",&p); del(p);
                }
                else if (z==3) printf("%d\n",a[poz[1]]);
            }
            fclose(stdin);
            fclose(stdout);
            return 0;
        }