Pagini recente » Cod sursa (job #166363) | Cod sursa (job #526347) | Cod sursa (job #3248441) | Cod sursa (job #857336) | Cod sursa (job #2494357)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int operatii,operatie,x,n,nh,poz[200200],v[200200],h[200200],tata,copil;
int main(){
fin>>operatii;
while(operatii!=0){
fin>>operatie;
if(operatie==1){
fin>>x;
n++;
v[n]=x;
nh++;
h[nh]=n;
poz[n]=nh;
copil=nh;
tata=copil/2;
while(tata>0){
if(v[h[copil]]<v[h[tata]]){
swap(h[copil],h[tata]);
poz[h[tata]]=tata;
poz[h[copil]]=copil;
}
else{
break;
}
copil=tata;
tata=tata/2;
}
}
if(operatie==2){
fin>>x;
v[x]=-1;
copil=poz[x];
tata=copil/2;
while(tata>0){
if(v[h[copil]]<v[h[tata]]){
swap(h[copil],h[tata]);
poz[h[tata]]=tata;
poz[h[copil]]=copil;
}
else{
break;
}
copil=tata;
tata=tata/2;
}
h[1]=h[nh];
poz[h[1]]=1;
nh--;
tata=1;
copil=2;
while(copil<=nh){
if(copil+1<=nh && v[h[copil+1]]<v[h[copil]]){
copil++;
}
if(v[h[copil]]<v[h[tata]]){
swap(h[copil],h[tata]);
poz[h[tata]]=tata;
poz[h[copil]]=copil;
}
else{
break;
}
tata=copil;
copil=2*copil;
}
}
if(operatie==3){
fout<<v[h[1]]<<"\n";
}
operatii--;
}
}