Pagini recente » Cod sursa (job #1160277) | Cod sursa (job #1397140) | Cod sursa (job #1118841) | Cod sursa (job #31985) | Cod sursa (job #1003852)
#include <fstream>
#include <algorithm>
using namespace std;
#define Nmax 200001
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int H[Nmax],poz[Nmax],A[Nmax],n,nr;
int op,i,a,b,j;
void afis()
{
for(j=1;j<=n;j++) g<<H[j]<<" ";g<<"\n";
for(j=1;j<=n;j++) g<<A[H[j]]<<" ";g<<"\n";
for(j=1;j<=nr;j++) g<<poz[j]<<" ";g<<"\n\n";
}
void perc(int x) //percolate
{
int xx=x/2;
while(x!=1 && A[H[x]]<A[H[xx]])
{
swap(H[x],H[xx]);
poz[x]=xx;
poz[xx]=x;
x=xx;
xx=x/2;
}
}
void sift(int x)
{
int xx=x*2,xy=xx+1,son;
do
{
son=0;
if(A[H[x]]>A[H[xx]] && xx<=n)
{
son=xx;
if(A[H[xx]]>A[H[xy]] && xy<=n) son=xy;
}
else if(A[H[x]]>A[H[xy]] && xy<=n) son=xy;
if(son)
{
swap(H[x],H[son]);
poz[x]=son;
poz[son]=x;
x=son;
xx=x*2;
xy=xx+1;
}
} while(son);
}
void sterg(int x)
{
H[x]=H[n];
poz[n]=x;
poz[x]=-1;
n--;
if(H[x]<H[x/2]) perc(x);
else sift(x);
}
void minim()
{
g<<A[H[1]]<<"\n";
}
int main()
{
f>>op;
for(i=1;i<=op;i++)
{
f>>a;
if(a==1)
{
f>>b;
A[++nr]=b;
H[++n]=nr;
poz[nr]=n;
perc(n);
}
else if(a==2)
{
f>>b;
A[b]=-1;
perc(poz[b]);
poz[H[1]]=0;
H[1]=H[n--];
poz[H[1]]=1;
if(n>1) sift(1);
}
else minim();
}
f.close();
g.close();
return 0;
}