Pagini recente » Cod sursa (job #2009341) | Cod sursa (job #268465) | Cod sursa (job #2010229) | Cod sursa (job #2036084) | Cod sursa (job #1919740)
#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;
aux=ord[p1];
ord[p1]=ord[p2];
ord[p2]=aux;
poz[ord[p1]]=p1;
poz[ord[p2]]=p2;
}
void up(int p)
{
while(p/2 && heap[p/2]>heap[p])
{
sch(p,p/2);
p/=2;
}
}
void down(int p)
{
while(2*p<=LH)
{
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)
{
sch(k,LH);
LH--;
heap[LH+1]=inf;
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;
}