Pagini recente » Cod sursa (job #848652) | Cod sursa (job #2819798) | Cod sursa (job #2650329) | Cod sursa (job #1274912) | Cod sursa (job #1919843)
#include <stdio.h>
#define N 200005
#define inf 1000000001
int n,c,i,x,LH,L;
int heap[N],poz[N],ord[N];
///heap[i]=valoarea care se afla in heap pe poz i; max i = LH
///poz[i]=pozitia din heap unde se afla elementul adaugat al i-lea; max i = L
///ord[i]=al catelea element este heap[i]; max i = LH
void sch(int p1,int p2)
{
int 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)
{
while(p/2>0 && heap[p/2]>heap[p])
{
sch(p,p/2);
p/=2;
}
}
void down(int p)
{
while(2*p<=LH && p>0)
{
if(heap[p]>heap[2*p])
{
if(heap[2*p]>heap[2*p+1] && 2*p+1<=LH)
{
sch(p,2*p+1);
p=p*2+1;
}
else
{
sch(p,2*p);
p*=2;
}
}
if(heap[p]>heap[2*p+1] && 2*p+1<=LH)
{
sch(p,2*p+1);
p=p*2+1;
}
}
}
void add(int x)
{
LH++;L++;
heap[LH]=x;
poz[L]=LH;
ord[LH]=L;
up(LH);
}
void del(int k)
{
heap[k]=inf;
sch(k,LH);
LH--;
down(k);
}
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",&c);
if(c==1)
{
fscanf(f1,"%d",&x);
add(x);
}
if(c==2)
{
fscanf(f1,"%d",&x);
del(poz[x]);
}
if(c==3)
{
fprintf(f2,"%d\n",heap[1]);
}
}
return 0;
}