Pagini recente » Cod sursa (job #1034169) | Cod sursa (job #2503706) | Profil Sagunistu | Cod sursa (job #830517) | Cod sursa (job #2629038)
#include <bits/stdc++.h>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int N = 100010;
int n,m,op,x,lg,v[N],h[N],p[N];
void heapDown(int),heapUp(int),heapSwap(int,int);
int main()
{
f>>n;
for(int i=1;i<=n;i++)
{
f>>op;
if(op==1)
{
f>>x;
v[++m]=x;
h[++lg]=m;
p[m]=lg;
heapUp(lg);
}
else
if(op==2)
{
f>>x;
x=p[x];
heapSwap(x,lg);
lg--;
heapUp(x);
heapDown(x);
}
else
{
g<<v[h[1]]<<'\n';
}
}
return 0;
}
void heapSwap(int i,int j)
{
swap(h[i],h[j]);
p[h[i]]=i;
p[h[j]]=j;
}
void heapUp(int F)
{
int T=F/2;
if(!T)return;
if(v[h[F]]<v[h[T]])
{
heapSwap(F,T);
heapUp(T);
}
}
void heapDown(int T)
{
int F = 2*T;
if(F>lg)return;
if(F<lg)if(v[h[F+1]]<v[h[F]])F++;
if(v[h[F]]<v[h[T]])
{
heapSwap(F,T);
heapDown(F);
}
}