Pagini recente » Cod sursa (job #2117186) | Cod sursa (job #938452) | Cod sursa (job #1813355) | Cod sursa (job #1007502) | Cod sursa (job #2388900)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int v[200001];
int h[200001];
int poz[200001];
int n,nh,i,x,y,citit;
void swapp(int p, int q){
swap(h[p],h[q]);
poz[h[p]]=p;
poz[h[q]]=q;
}
void urca(int p){
while(p>1 && v[h[p]]<v[h[p/2]])
{
swapp(p,p/2);
p/=2;
}
}
void coboara(int p){
int fs=2*p,fd=2*p+1,bun=p;
if(fs<=nh && h[fs]<h[bun]){
bun=fs;
}
if(fd<=nh && h[fd]<h[bun]){
bun=fd;
}
if(bun!=p){
swapp(p,bun);
coboara(bun);
}
}
void adauga(int p){
h[++nh]=p;
poz[p]=nh;
urca(nh);
}
void sterge(int p){
if(p==nh){
nh--;
return;
}
h[p]=h[nh--];
urca(p);
coboara(p);
}
int main()
{
in>>n;
for(i=1 ; i<=n ; i++){
in>>x;
if(x==1){
in>>y;
v[++citit]=y;
adauga(citit);
}
if(x==2){
in>>y;
sterge(poz[y]);
}
if(x==3){
out<<v[h[1]]<<"\n";
}
}
return 0;
}