Pagini recente » Istoria paginii utilizator/dreigogos | Cod sursa (job #1551824) | Cod sursa (job #200668) | Cod sursa (job #1580980) | Cod sursa (job #1296337)
#include <fstream>
#define dim 200003
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int v[dim],n,i,o,k,x,poz[dim],j,h[dim],m;
void insereaza(int x){
h[++k]=x;
poz[x]=k;
int c=k;
int p=k/2;
while(p>0){
if(v[h[p]]>v[h[c]]){
swap(h[p],h[c]);
swap(poz[h[c]],poz[h[p]]);
c=p;
p/=2;
}
else
break;
}
}
void sterge_radacina(){
h[1]=h[k];
poz[h[k]]=1;
k--;
int p=1,c=2;
while(c<=k){
if(c+1<=k && v[h[c+1]]<v[h[c]])
c++;
if(v[h[p]]>v[h[c]]){
swap(h[c],h[p]);
swap(poz[h[c]],poz[h[p]]);
p=c;
c*=2;
}
else
break;
}
}
void sterge(int x){
v[x]=-1;
int c=poz[x];
int p=c/2;
while(p>0){
if(v[h[p]]>v[h[c]]){
swap(h[c],h[p]);
swap(poz[h[c]],poz[h[p]]);
c=p;
p/=2;
}
else
break;
}
sterge_radacina();
}
int main(){
fin>>n;
while(n--){
fin>>o;
if(o==1){
fin>>v[++m];
insereaza(m);
continue;
}
if(o==2){
fin>>x;
sterge(x);
continue;
}
if(o==3){
fout<<v[h[1]]<<'\n';
}
}
fin.close();fout.close();
return 0;
}