Pagini recente » Cod sursa (job #2741902) | Cod sursa (job #2344058) | Cod sursa (job #2515706) | Cod sursa (job #809209) | Cod sursa (job #2466433)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int i,x,c,loc[200005],n,m,poz,intrat;
struct heap{int first,second;};
heap h[200005];
void suap(int x,int y)
{
swap(h[x],h[y]);
loc[h[x].second]=x;
loc[h[y].second]=y;
}
void HeapUp(int k)
{
if(k>1)
{
int t=k/2;
if(h[t].first>h[k].first)
{
suap(t,k);
HeapUp(t);
}
}
}
void HeapDown(int k)
{
int st=0,dr=0;
if(2*k<=n)
{
st=2*k;
if(2*k+1<=n)
dr=2*k+1;
if(h[st].first<h[dr].first || dr==0)
{
if(h[k].first>h[st].first)
{
suap(k,st);
HeapDown(st);
}
}
else if(h[k].first>h[dr].first)
{
suap(k,dr);
HeapDown(dr);
}
}
}
int main()
{
f>>m;
for(i=1; i<=m; i++)
{
f>>c;
if(i==33)
i=33;
if(c==1)
{
f>>x;
if(x==12)
x=12;
n++;
intrat++;
h[n].first=x;
h[n].second=intrat;
loc[intrat]=n;
HeapUp(n);
}
else if(c==2)
{
f>>x;
poz=loc[x];
h[poz]=h[n];
n--;
HeapDown(poz);
}
else if(c==3)
{
g<<h[1].first<<'\n';
}
}
return 0;
}