Pagini recente » Cod sursa (job #1723197) | Cod sursa (job #946934) | Cod sursa (job #3122380) | Cod sursa (job #1951750) | Cod sursa (job #1514483)
#include <iostream>
#include <fstream>
#define N 200010
using namespace std;
long nh,nr,v[N],h[N],pos[N],k; //nr=nr de elem citite; nh=nr de elem din heap, dupa stergeri
void schimb(int p, int q)
{
int aux=h[p];
h[p]=h[q];
h[q]=aux;
pos[h[p]]=p;
pos[h[q]]=q;
}
void urca(int p)
{
while(p>1 && v[h[p]]<v[h[p/2]])
{
schimb(p,p/2);
p/=2;
}
}
void coboara(int p)
{
int fs=2*p,fd=2*p+1, bun=p;
if(fs<=nh && v[h[fs]]<v[h[bun]])
bun=fs;
if(fd<=nh && v[h[fd]]<v[h[bun]])
bun=fd;
if(bun!=p)
{
schimb(bun,p);
coboara(bun);
}
}
int main()
{
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int i,p,m,x,cod;
fin>>m;
for(i=1;i<=m;i++)
{
fin>>cod;
if(cod==1)
{
fin>>v[++nr];
h[++nh]=nr; //adauga
pos[nr]=nh;
urca(nh);
}
if(cod==2)
{
fin>>x;
p=pos[x]; //sterge al x-lea elem
schimb(p,nh--);
urca(p);
coboara(p);
}
if(cod==3)
fout<<v[h[1]]<<endl;
}
return 0;
}