Pagini recente » Cod sursa (job #2873291) | Cod sursa (job #1441770) | Cod sursa (job #1270234) | Cod sursa (job #2337125) | Cod sursa (job #1205224)
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream o("heapuri.out");
int h[200200],poz[200200],a[200200],hp,n,k=0;
void urca(int x){
int fiu = x, tata = x/2;
while(a[h[fiu]]<a[h[tata]] && tata){
swap(poz[h[tata]],poz[h[fiu]]);
swap(h[fiu],h[tata]);
fiu=tata;
tata = fiu/2;
}
}
void add(int v){
hp++;k++;
a[k]=v; h[hp]=k; poz[k]=hp;
urca(hp);
}
void coboara(int x){
int fiu = x*2,tata=x;
while(fiu<=hp){
if(a[h[fiu+1]]<a[h[fiu]] && fiu+1<=hp)fiu++;
if(a[h[fiu]]<a[h[tata]]){
swap(poz[h[tata]],poz[h[fiu]]);
swap(h[fiu],h[tata]);
tata = fiu;
fiu=fiu*2;
}else fiu = hp+1;
}
}
void sterge(int x){
poz[h[hp]] = x;
h[x] = h[hp];
hp--;
if(x==hp+1)return;
if(a[h[x]]<a[h[x/2]] && x/2>0)urca(x);
coboara(x);
}
int main(){
f>>n;
int x,y;
for(int i=1;i<=n;i++){
f>>x;
if(x==1){
f>>y; add(y);
}else if(x==2){
f>>y; sterge(poz[y]);
}else o<<a[h[1]]<<"\n";
}
}