Cod sursa(job #765443)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 7 iulie 2012 18:08:31
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include<cstdio>
int h[200001],n,k,i,r,j,p[200001],v[200001],z;

void A(int *n,int j)
{h[++(*n)]=j,p[j]=(*n);
int t=(*n),k=h[t],x;
while(t>1&&v[k]<v[h[t/2]])
      x=h[t],h[t]=h[t/2],h[t/2]=x,x=p[h[t]],p[h[t]]=p[h[t/2]],p[h[t/2]]=x,t/=2;}

void D(int *n,int k)
{int s,x,t=h[k];
h[k]=h[(*n)],h[(*n)]=t,x=p[h[(*n)]],p[h[(*n)--]]=p[h[k]],p[h[k]]=x,x=h[k];
while(k>1&&v[x]<v[h[k/2]])
      t=p[h[k]],p[h[k]]=p[h[k/2]],p[h[k/2]]=t,t=h[k],h[k]=h[k/2],h[k/2]=t,k/=2;
do
      {s=0;
      if(2*k<=(*n))
            {s=2*k;
            if(s<(*n)&&v[h[2*k+1]]<v[h[2*k]])
                   s=2*k+1;
            if(v[h[s]]>=v[h[k]])
                   s=0;}
      if(s)
            x=h[k],h[k]=h[s],h[s]=x,t=p[h[s]],p[h[s]]=p[h[k]],p[h[k]]=t,k=s;}
while(s);}

int main()
{freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
while(n--)
      {scanf("%d",&z);
      if(z==3)
              printf("%d\n",v[h[1]]);
      else
              {scanf("%d",&r);
              if(z==1)
                      v[++j]=r,A(&k,j);
              else
                      D(&k,p[r]);}}
return 0;}