Pagini recente » Cod sursa (job #2018508) | Cod sursa (job #1536638) | Diferente pentru teorema-chineza-a-resturilor intre reviziile 52 si 51 | Monitorul de evaluare | Cod sursa (job #1276053)
#include<fstream>
using namespace std;
int T, op, a[200003], h[200003], n, dh, poz[200003], x, c, p;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int main(){
in>>T;
for(; T-- ;){
in>>op;
if(op==3){
out<<a[h[1]]<<"\n";
continue;
}
if(op==1){
in>>x;
a[++n]=x;
h[++dh]=n;
poz[n]=dh;
c=dh;
p=dh/2;
while(p>0){
if(a[h[c]]<a[h[p]]){
swap(h[c], h[p]);
poz[h[c]]=c;
poz[h[p]]=p;
}else
break;
c=p;
p/=2;
}
continue;
}
in>>x;
a[x]=-1;
c=poz[x];
p=c/2;
while(p>0){
if(a[h[c]]<a[h[p]]){
swap(h[c], h[p]);
poz[h[c]]=c;
poz[h[p]]=p;
}else
break;
c=p;
p/=2;
}
h[1]=h[dh];
poz[h[1]]=1;
dh--;
p=1;
c=2;
while(c<=dh){
if(c+1<=dh && a[h[c+1]]<a[h[c]])
c++;
if(a[h[c]]<a[h[p]]){
swap(h[c], h[p]);
poz[h[c]]=c;
poz[h[p]]=p;
}else
break;
p=c;
c*=2;
}
}
return 0;
}