Pagini recente » Cod sursa (job #2518454) | Cod sursa (job #748847) | Cod sursa (job #2202478) | Cod sursa (job #2447491) | Cod sursa (job #1810498)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct heap
{
int x,ind;
}h[200010];
int nr;
char vaz[200010];
void up_heap(int nod)
{
while(nod>1 && h[nod/2].x>h[nod].x)
{
swap(h[nod],h[nod/2]);
nod=nod/2;
}
}
void down_heap(int nod)
{
while((nod*2<=nr && h[nod].x>h[nod*2].x) || (nod*2+1<=nr && h[nod].x>h[nod*2+1].x))
{
if(nod*2==nr || h[nod*2].x<h[nod*2+1].x)
{
swap(h[nod],h[nod*2]);
nod=nod*2;
}
else
{
swap(h[nod],h[nod*2+1]);
nod=nod*2+1;
}
}
}
int main()
{
int n,cnt=0,tip,x;
fin>>n;
for(int i=1;i<=n;i++)
{
fin>>tip;
if(tip==1)
{
fin>>x;
h[++nr]={x,++cnt};
up_heap(nr);
}
else if(tip==2)
{
fin>>x;
vaz[x]=1;
}
else
{
while(vaz[h[1].ind])
{
h[1]=h[nr];
nr--;
down_heap(1);
}
fout<<h[1].x<<"\n";
}
}
return 0;
}