Pagini recente » Borderou de evaluare (job #2772025) | Cod sursa (job #189556) | Cod sursa (job #1935421) | Cod sursa (job #1273681) | Cod sursa (job #305404)
Cod sursa(job #305404)
#include<fstream.h>
int N,n,P,s[200001],H[200001],p[200001];//n - dimensiunea heap-ului,P - nr maxim de ordine cronoligica a elementelor intrate, p - sirul cu pozitia in heap a elementelor din s in ordine cronologica
inline void swap(int &x,int &y)
{int z=x;x=y;y=z;}
void perc(int k)
{while(k>1&&s[H[k]]<s[H[k/2]])
{swap(H[k],H[k/2]);
p[H[k]]=k;
k/=2;
p[H[k]]=k;}
}
void sift(int k)
{int f;
do
{f=0;
if(2*k<=n)
f=2*k;
if(2*k+1<=n&&s[H[f]]>s[H[2*k+1]])
f=2*k+1;
if(H[f]>=H[k])
f=0;
if(f)
{swap(H[k],H[f]);
p[H[k]]=k;
p[H[f]]=f;}}
while(f);
}
int main()
{ifstream f("heapuri.in");
ofstream g("heapuri.out");
short cod;int x,k;
f>>N;
for(;N--;)
{f>>cod;
if(cod<3)
{f>>x;
if(cod<2)
{s[++P]=x;
H[++n]=P;
p[P]=n;
perc(n);}
else
{k=p[x];H[k]=H[n--];
p[H[k]]=k;
if(k>1&&H[k]<H[k/2])
perc(k);
else
sift(k);
}
}
else g<<s[H[1]]<<'\n';
}
return 0;
}