Cod sursa(job #1773806)

Utilizator GandalfTheWhiteGandalf the White GandalfTheWhite Data 8 octombrie 2016 11:23:59
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <algorithm>
#define N 200005
using namespace std;
int h[N],poz[N],a[N],l,n,nr;
void push(int x)
{
while (x/2 && a[h[x]]<a[h[x/2]])
   {
   swap(h[x],h[x/2]);
   poz[h[x]]=x;
   poz[h[x/2]]=x/2;
   x/=2;
   }
}
void cut(int x)
{
int y=0;
while (x!=y)
   {
   y=x;
   if (y*2<=l && a[h[x]]>a[h[y*2]]) x=y*2;
   if (y*2+1<=l && a[h[x]]>a[h[y*2+1]]) x=y*2+1;
   swap(h[x],h[y]);
   poz[h[x]]=x;
   poz[h[y]]=y;
   }
}
int main()
{
int i,c,x;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for (i=1;i<=n;i++)
   {
   scanf("%d",&c);
   if (c==1) {
               scanf("%d",&x);
               l++;nr++;
               a[nr]=x;
               h[l]=nr;
               poz[nr]=l;
               push(l);
               }
   if (c==2) {
               scanf("%d",&x);
               a[x]=-1;
               push(poz[x]);
               poz[h[1]]=0;
               h[1]=h[l--];
               poz[h[1]]=1;
               if (l>1) cut(1);
               }
   if (c==3) printf("%d\n",a[h[1]]);
   }
}