Pagini recente » Cod sursa (job #2657717) | Cod sursa (job #3127271) | Cod sursa (job #606822) | Cod sursa (job #265451) | Cod sursa (job #2925287)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int v[200002], poz[200002], h[200002], k, nrHeap, m, operatie, x;
void upHeap(int k)
{
while(k>1 && v[h[k]]<v[h[k/2]])
{
int aux=h[k/2];
h[k/2]=h[k];
h[k]=aux;
k=k/2;
poz[h[k]]=k;
poz[h[k/2]]=k/2;
k/=2;
}
}
void downHeap(int k)
{
while(2*k<=nrHeap)
{
int pozitie=2*k;
if(2*k+1<=nrHeap && v[h[2*k+1]]<v[h[pozitie]])
{
pozitie=2*k+1;
}
if(v[h[pozitie]]<v[h[k]])
{
int aux=h[pozitie];
h[pozitie]=h[k];
h[k]=aux;
k=pozitie;
poz[h[pozitie]]=pozitie;
poz[h[k]]=k;
}
else{break;}
}
}
int main()
{
fin>>m;
for(int i=1; i<=m; i++)
{
fin>>operatie;
if(operatie==1)
{
fin>>x;
v[++k]=x;
h[++nrHeap]=k;
upHeap(nrHeap);
}
if(operatie==2)
{
fin>>x;
v[x]=-1;
upHeap(poz[x]);
h[1]=h[nrHeap--];
poz[h[1]]=1;
downHeap(1);
}
if(operatie==3)
{
fout<<v[h[1]]<<"\n";
}
}
return 0;
}