Pagini recente » Cod sursa (job #1422360) | Cod sursa (job #1725013) | Cod sursa (job #2972998) | Cod sursa (job #2075523) | Cod sursa (job #2156224)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,cod,x,cron[200005],HS,heap[200005],poz[200005],HC;
void afis()
{
for(int i=1;i<=HS;i++) fout<<heap[i]<<' ';
fout<<'\n';
}
void peek()
{
fout<<heap[1]<<'\n';
}
void swapp(int x, int y)
{
swap(heap[x],heap[y]);
swap(poz[heap[x]],poz[heap[y]]);
}
int bubble_up(int x)
{
if(heap[x]<heap[x/2])
{
swapp(x,x/2);
bubble_up(x/2);
}
}
int bubble_down(int x)
{
int ls=2*x;
int rs=2*x+1;
int s=0;
if(rs>HS)
{
if(ls<=HS) s=ls;
else return 0;
}
else
{
if(heap[rs]<heap[ls]) s=rs;
else s=ls;
}
if(heap[x]>heap[s])
{
swapp(x,s);
bubble_down(s);
}
}
void insereaza(int x)
{
HC++;
HS++;
poz[x]=HS;
heap[HS]=x;
cron[HC]=x;
bubble_up(HS);
}
void sterge(int x)
{
int p=poz[x];
swapp(HS,p);
heap[HS]=0; poz[heap[HS]]=0; HS--;
bubble_down(p);
bubble_up(p);
}
int main()
{
fin>>n;
for(int i=1;i<=n;i++)
{
fin>>cod;
if(cod==1)
{
fin>>x;
insereaza(x);
}
else if(cod==2)
{
fin>>x;
sterge(cron[x]);
}
else peek();
}
return 0;
}