Cod sursa(job #240985)

Utilizator zbarniZajzon Barna zbarni Data 9 ianuarie 2009 02:08:34
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<stdio.h>
#define nmax 200005
int pos[nmax],n,l;
struct heap
 {
  int val,poz;
 };
heap a[nmax];

void comb_heap (int i, int n)
 {
  int v=a[i].val,tata=i,fiu=2*i;
  heap aux;
  while (fiu<=n)
   {
    if (fiu<n)
      if (a[fiu].val>a[fiu+1].val) fiu++;
    if (v>a[fiu].val)
     {
      aux=a[tata];
      a[tata]=a[fiu];
      a[fiu]=aux;

      pos[a[fiu].poz]=tata;
      pos[a[tata].poz]=fiu;

      tata=fiu;
      fiu=fiu*2;
     }
    else
      fiu=n+1;
   }

 }

void push (int x)
 {
  int fiu=++n,tata=n/2;
  a[n].val=x,a[n].poz=l;
  pos[l]=n;
  heap aux;
  while (tata && a[tata].val>x)
   {
    aux=a[tata];
    a[tata]=a[fiu];
    a[fiu]=aux;

    pos[a[tata].poz]=tata;
    pos[a[fiu].poz]=fiu;
    fiu=tata;
    tata/=2;
   }
 }

void pop (int x)
 {
  a[pos[x]]=a[n--];
  pos[a[n+1].poz]=pos[x];
  pos[x]=0;
  comb_heap(pos[l],n);
 }

int main()
 {
  freopen("heapuri.in","r",stdin);
  freopen("heapuri.out","w",stdout);
  int nr,x,c;
  scanf("%d",&nr);
  int i;
  for (i=1;i<=nr;++i)
   {
    scanf("%d",&c);
    if (c!=3)
      scanf("%d",&x);
    if (c==1)
      {
       ++l;
       push(x);
      }
    if (c==2)
      {
       pop(x);
      }
    if (c==3)
      printf ("%d\n",a[1].val);
   }
  return 0;
 }