Pagini recente » Cod sursa (job #1980200) | Cod sursa (job #349418) | Cod sursa (job #1821950) | Cod sursa (job #2206294) | Cod sursa (job #2268772)
# include<cstdio>
# include<algorithm>
const int NMAX = 200000;
int h[NMAX + 1];
int poz[NMAX + 1];
int v[NMAX + 1];
int nh;
void swapp(int p , int q){
std :: 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 adauga(int val)
{
h[++nh] = val;
poz[val] = nh;
urca(nh);
}
void coboara(int p)
{
int fs = 2 * p , fd = 2 * p + 1 , bun = p;
if( fs <= nh && v[ h [ fs ] ] < v [ h [ bun ] ])
{
bun = fs;
}
if(fs <= nh && v[ h[fd] ] <= v[ h [ bun] ])
{
bun = fd;
}
if(bun != p){
swapp(p , bun);
coboara(bun);
}
}
void sterge(int p){
h[p] = h[nh --];
coboara(p);
urca(p);
}
int main()
{
int n , tip , val , nr = 0, p;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i ++)
{
scanf("%d" , &tip);
if(tip == 1)
{
scanf("%d", &val);
v[ ++nr ] = val;
adauga( nr );
}
if(tip == 2)
{
scanf("%d" , &p);
sterge(poz[p]);
}
if(tip == 3)
{
printf("%d\n" , v[h[1]]);
}
}
}