Pagini recente » Cod sursa (job #1375219) | Cod sursa (job #1075792) | Cod sursa (job #919225) | Cod sursa (job #1476949) | Cod sursa (job #1922762)
#include <stdio.h>
#define N 200020
int n,i,q,x,lh,l;
int heap[N],ord[N],poz[N];
void sch(int p1,int p2)
{
int aux;
aux=heap[p1];
heap[p1]=heap[p2];
heap[p2]=aux;
poz[ord[p1]]=p2;
poz[ord[p2]]=p1;
aux=ord[p1];
ord[p1]=ord[p2];
ord[p2]=aux;
}
void up(int p)
{
if(heap[p]<heap[p/2])
{
sch(p,p/2);
up(p/2);
}
}
void down(int p)
{
if(2*p<=lh && heap[2*p]<heap[p])
{
if(heap[2*p+1]<heap[2*p] && 2*p+1<=lh)
{
sch(2*p+1,p);
down(2*p+1);
}
else
{
sch(2*p,p);
down(2*p);
}
}
else if(2*p+1<=lh && heap[2*p+1]<heap[p])
{
sch(2*p+1,p);
down(2*p+1);
}
}
void add(int x)
{
lh++;l++;
heap[lh]=x;
ord[lh]=l;
poz[l]=lh;
up(lh);
}
void del(int p)
{
sch(p,lh);
lh--;
down(p);
}
int main()
{
FILE *f1,*f2;
f1=fopen("heapuri.in","r");
f2=fopen("heapuri.out","w");
fscanf(f1,"%d",&n);
for(i=0;i<n;i++)
{
fscanf(f1,"%d",&q);
if(q==3)
fprintf(f2,"%d\n",heap[1]);
else
{
fscanf(f1,"%d",&x);
if(q==1)
{
add(x);
}
if(q==2)
{
del(poz[x]);
}
}
}
return 0;
}