Cod sursa(job #1085460)

Utilizator AllxCucuCucu Alexandru AllxCucu Data 17 ianuarie 2014 00:13:45
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
			#include <fstream>
#include <assert.h>
#define nmax 200005
using namespace std;
 ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
int n,l,nr;
int heap[nmax],v[nmax],poz[nmax];
int push(int x)
{
int aux;
while(x/2 && v[heap[x]]<v[heap[x/2]])
{
aux=heap[x];
heap[x]=heap[x/2];
heap[x/2]=aux;
poz[heap[x]]=x;
poz[heap[x/2]]=x/2;
x=x/2;
}
return 0;
}
int pop(int x)
{
int aux,y=0;
while(x!=y)
{
y=x;
if(y*2<=l && v[heap[x]]>v[heap[y*2]]) x=y*2;
if(y*2+1<=l && v[heap[x]]>v[heap[y*2+1]]) x=y*2+1;
aux=heap[x];
heap[x]=heap[y];
heap[y]=aux;
poz[heap[x]]=x;
poz[heap[y]]=y;
}
return 0;
}
int main()
{
int x,i,c;
cin>>n;
;
 

for(i=1; i<=n; i++)
{
cin>>c;
if(c==1)
{
cin>>x;
l++;
nr++;
v[nr]=x;
heap[l]=nr;
poz[nr]=l;
push(l);
}
if(c==2)
{
cin>>x;
v[x]=-1;
 -1;

assert(1<=x && x<=nr);
push(poz[x]);
poz[heap[1]]=0;
heap[1]=heap[l--];
poz[heap[1]]=1;
if(l>1) pop(1);
}
if(c==3) cout<<v[heap[1]]<<'\n';
}
return 0;
}