Pagini recente » Cod sursa (job #1795658) | Cod sursa (job #708829)
Cod sursa(job #708829)
#include<cstdio>
#define nmax 200001
int n,cod,x,heap[nmax],poz[nmax],it[nmax],nr,nrpoz;
void insert(int);
void del(int);
int main()
{
int i;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d", &n);
for(i=1;i<=n;i++)
{
scanf("%d", &cod);
if(cod<3)
{
scanf("%d", &x);
if(cod==1)
insert(x);
else
del(x);
}
else
printf("%d\n", heap[1]);
}
return 0;
}
void insert(int val)
{
int child, dad,aux;
child=++nr;
heap[child]=val;
it[child]=nr;
poz[child]=++nrpoz;
for(;;)
if(child==1)
return;
else
{
dad=child/2;
if(heap[dad]<=heap[child])
return;
else
{
aux=heap[dad];
heap[dad]=heap[child];
heap[child]=aux;
aux=it[dad];
it[dad]=it[child];
it[child]=aux;
poz[it[dad]]=dad;
poz[it[child]]=child;
child=dad;
}
}
}
void del(int val)
{
heap[poz[val]]=heap[nr];
it[poz[val]]=it[nr];
nr--;
int dad=poz[val];
int child=dad*2,aux;
poz[val]=-1;
while(child<=nr)
{
if(child<nr)
if(heap[child]>heap[child+1])
child++;
if(heap[dad]<=heap[child])
return;
else
{
aux=heap[dad];
heap[dad]=heap[child];
heap[child]=aux;
aux=it[dad];
it[dad]=it[child];
it[child]=aux;
poz[it[dad]]=dad;
poz[it[child]]=child;
dad=child;
child=2*child;
}
}
}