Pagini recente » Cod sursa (job #398769) | Cod sursa (job #2511784) | Cod sursa (job #288971) | Cod sursa (job #48263) | Cod sursa (job #1565987)
#include <fstream>
#define lg Heap[0]
using namespace std;
ifstream cin("heap.in");
ofstream cout("heap.out");
int n,i,j;
int Heap[200003],Poz[200003],Ind[200003];
int TA(int x){return x/2;}
int FS(int x){return x*2;}
int FD(int x){return x*2+1;}
int Promovare(int nod, int x)
{
while(nod>1 && x<Heap[TA(nod)])
{
Heap[nod]=Heap[TA(nod)];
Poz[Heap[TA(nod)]]=nod;
nod=TA(nod);
}
Heap[nod]=x;
Poz[x]=nod;
return nod;
}
void Retrogradare(int nod,int x)
{
int aux=0,aux1=0;
while(FS(nod)<=lg)
{
if(FD(nod)<=lg)
{
if(Heap[FS(nod)]<Heap[FD(nod)])
nod=FS(nod);
else
nod=FD(nod);
}
else
nod=FS(nod);
if(Heap[nod]>Heap[TA(nod)])
nod=lg+1;
if(nod<=lg)
{
aux=Heap[nod];
aux1=Poz[Heap[nod]];
Heap[nod]=Heap[TA(nod)];
Poz[Heap[nod]]=Poz[Heap[TA(nod)]];
Heap[TA(nod)]=aux;
Poz[Heap[TA(nod)]]=aux1;
}
}
}
int Insert_H(int x)
{
Heap[++lg]=x;
Poz[x]=lg;
return Promovare(lg,x);
}
void Erase_H(int x)
{
Heap[x]=Heap[lg];
--lg;
if(x>1 && Heap[x]<Heap[TA(x)])
Promovare(x,Heap[x]);
else
Retrogradare(x,Heap[x]);
}
int main()
{
cin>>n;
for(int z=1;z<=n;++z)
{
cin>>i;
if(i==1)
{
cin>>j;
Ind[++Ind[0]]=j;
Insert_H(j);
}
if(i==2)
{
cin>>j;
Erase_H(Poz[Ind[j]]);
}
if(i==3)
cout<<Heap[1]<<'\n';
}
return 0;
}