Cod sursa(job #765451)

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

void A(int t)
{int y;
while(t>1&&v[h[t]]<v[h[t/2]])
      y=h[t],h[t]=h[t/2],h[t/2]=y,p[h[t]]=t,p[h[t/2]]=t/2,t/=2;}

void D(int x)
{int z,y=0;
while(x!=y)
     {y=x;
     if(2*y<=j&&v[h[x]]>v[h[2*y]])
           x=2*y;
     if(2*y<j&&v[h[x]]>v[h[2*y+1]])
           x=2*y+1;
     z=h[x],h[x]=h[y],h[y]=z,p[h[x]]=x,p[h[y]]=y;}}

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,p[j]=h[j]=j,A(j);
              else
                      
                      {A(p[r]),p[h[1]]=0,h[1]=h[j--],p[h[1]]=1;
                      if(j>1)
                              D(1);}}}
return 0;}