Pagini recente » Cod sursa (job #2079773) | Cod sursa (job #2888853) | Rating qwerty (sebi95) | Cod sursa (job #1266890) | Cod sursa (job #2757948)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int v[200001], h[200001], poz[200001];
int n,nh;
void schimba(int x, int y)
{
swap(h[x],h[y]);
poz[h[x]]=x;
poz[h[y]]=y;
}
void urca(int p)
{
while(p>1 && v[h[p]]<v[h[p/2]]){
schimba(p,p/2);
p/=2;
}
}
void adauga(int x)
{
h[++nh]=x;
poz[x]=nh;
urca(nh);
}
void coborare(int p)
{
int fs,fd,bun;
fs=2*p;
fd=2*p+1;
bun=p;
if(fs<=nh && v[h[fs]]<v[h[bun]])
bun=fs;
if(fd<=nh && v[h[fd]]<v[h[bun]])
bun=fd;
if(bun!=p){
schimba(p,bun);
coborare(bun);
}
}
void sterge(int p)
{
if(p==nh)
nh--;
else{
h[p]=h[nh--];
poz[h[p]]=p;
urca(p);
coborare(p);
}
}
int main()
{
int nr;
in>>nr;
for(int i=0;i<nr;i++){
int op;
in>>op;
if(op==1){
in>>v[++n];
adauga(n);
}
if(op==2){
int val;
in>>val;
sterge(poz[val]);
}
if(op==3)
out<<v[h[1]]<<'\n';
}
return 0;
}