Pagini recente » Cod sursa (job #1949621) | Cod sursa (job #1876798) | Cod sursa (job #2722255) | Cod sursa (job #2756425) | Cod sursa (job #2695812)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int nqmax=200000;
int h[nqmax+1],hn=0, p[nqmax+1], a[nqmax+1];
void hswap(int x, int y){
int aux=h[x];
h[x]=h[y];
h[y]=aux;
aux=a[x];
a[x]=a[y];
a[y]=aux;
aux=p[a[x]];
p[a[x]]=p[a[y]];
p[a[y]]=aux;
}
void hpercolate(int x){
if(x>1&&h[x/2]>h[x]){
hswap(x/2,x);
hpercolate(x/2);
}
}
void hsieve(int x){
if(hn>=2*x+1&&(h[x*2+1]<h[x]||h[x*2]<h[x])){
if(h[x*2+1]>h[x*2]){
hswap(x,x*2);
hsieve(x*2);
}else{
hswap(x,x*2+1);
hsieve(x*2+1);
}
}else if(hn>=2*x&&h[x*2]<h[x]){
hswap(x*2,x);
hsieve(x*2);
}
}
int main(){
int nq;
fin>>nq;
int pn=0;
for(int cq=1;cq<=nq;cq++){
int x;
fin>>x;
if(x==1){
int y;
fin>>y;
hn++;
h[hn]=y;
pn++;
p[pn]=hn;
a[hn]=pn;
hpercolate(hn);
}else if(x==2){
int y;
fin>>y;
int y2=p[y];///y2 reprezinta pozitia in heap al y-lea nr intrat.
hswap(y2,hn);
hn--;
hpercolate(y2);
hsieve(y2);
}else{
fout<<h[1]<<"\n";
}
}
return 0;
}