Pagini recente » Cod sursa (job #2070315) | Cod sursa (job #1827515) | Cod sursa (job #911590) | Cod sursa (job #1351823) | Cod sursa (job #708859)
Cod sursa(job #708859)
#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]=++nrpoz;
poz[nrpoz]=nr;
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)
{
int y=poz[val];
heap[y]=heap[nr];
it[y]=it[nr];
nr--;
int dad,child,aux;
dad=y;
child=dad*2;
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=child*2;
}
}
}