Pagini recente » Cod sursa (job #564458) | Cod sursa (job #2319648) | Cod sursa (job #2469993) | Cod sursa (job #2474111) | Cod sursa (job #825598)
Cod sursa(job #825598)
#include<cstdio>
using namespace std;
const int NMAX=200001;
int v[NMAX],u,poz[NMAX],num,max,h[NMAX];
FILE *f=fopen("heapuri.in","r");
FILE *g=fopen("heapuri.out","w");
void swap (int &a,int &b)
{
int aux;
aux=a;
a=b;
b=aux;
}
void upheap (int nod)
{
if (nod>1)
if (v[h[nod/2]]>v[h[nod]])
{swap(h[nod/2],h[nod]);
poz[h[nod]]=nod;
poz[h[nod/2]]=nod/2;
upheap(nod/2);
}
}
void downheap( int nod)
{
int fiu;
if (nod*2<=u) {
fiu=nod*2;
if (nod*2+1<=u && v[h[fiu]]>v[h[nod*2+1]])
fiu=fiu+1;
if (v[h[fiu]]<v[h[nod]]) {
swap(h[fiu],h[nod]);
poz[h[fiu]]=fiu;
poz[h[nod]]=nod;
downheap(fiu);
}
}
}
void inserare(int x)
{
v[++u]=x;h[u]=u; poz[u]=u;
upheap(u);
}
void stergere(int x)
{
v[x]=2000000000;
downheap(poz[x]);
}
void afisare()
{
fprintf(g,"%d\n",v[h[1]]);
}
int main()
{
int n,i,op,x;
fscanf(f,"%d",&n);
for (i=1;i<=n;++i)
{
fscanf(f,"%d",&op);
if (op==1){fscanf(f,"%d",&x);
inserare(x);
}
if (op==2){fscanf(f,"%d",&x);
stergere(x);
}
if (op==3)
afisare();
}
return 0;
}