Pagini recente » Cod sursa (job #1775181) | Cod sursa (job #1320314) | Cod sursa (job #3001314) | Cod sursa (job #1114239) | Cod sursa (job #1204440)
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream o("heapuri.out");
int n,hp=0,poz[210000],h[200020],a[200200];
void urca(int x){
int tata=poz[x]/2,fiu=poz[x];
while(tata && a[h[tata]]>a[h[fiu]]){
swap(poz[h[tata]],poz[h[fiu]]);
swap(h[tata],h[fiu]);
fiu=tata;
tata=tata/2;
}
}
void adauga(int x){
hp++;
poz[hp]=hp;
h[hp]=hp;
urca(x);
}
void coboara(int x){
//o<<x;
int tata=poz[x],fiu=poz[x]*2;
while(fiu<=hp){
if(a[h[fiu]]>a[h[fiu+1]]&&fiu+1<=hp)fiu++;
if(a[h[fiu]]<a[h[tata]]){
swap(poz[h[tata]],poz[h[fiu]]);
swap(h[tata],h[fiu]);
tata=fiu;
fiu=fiu*2;
}else fiu=hp+1;
}
}
void sterge(int x){
int val = a[h[hp]];
int id = h[hp];
poz[h[hp]] = poz[x];
h[poz[x]] = h[hp];
hp--;
if(val<a[h[poz[x]/2]] && poz[x]!=1)urca(id);
else coboara(id);
}
int main(){
f>>n;int x,y,j=0;
for(int i=1;i<=n;i++){
f>>x;
if(x==1){
++j;
f>>a[j];
adauga(j);
}else if(x==2){
f>>y;sterge(y);
}else o<<a[h[1]]<<"\n";
}
}