Pagini recente » Cod sursa (job #2323519) | Cod sursa (job #2360613) | Cod sursa (job #2728509) | Cod sursa (job #1550491) | Cod sursa (job #360401)
Cod sursa(job #360401)
#include <stdio.h>
#define nmax 200001
typedef struct { int key;long x; } heap;
heap H[nmax];
int k,nh,poz[nmax];
void swap(int a,int b)
{
heap c=H[a];
H[a]=H[b];
H[b]=c;
poz[H[a].key]=a;
poz[H[b].key]=b;
}
void upheap(int nod)
{
if(nod<=1) return;
int i=nod>>1;
if(H[nod].x<H[i].x)
{
swap(nod,i);
upheap(i);
}
}
void downheap(int nod)
{
int i=nod<<1;
if(i<=nh)
{
if(i+1<=nh&&H[i+1].x<H[i].x)
++i;
if(H[i].x<H[nod].x)
{
swap(i,nod);
downheap(i);
}
}
}
int main()
{
int m,i,caz,y;
FILE *f=fopen("heapuri.in","r");
FILE *g=fopen("heapuri.out","w");
fscanf(f,"%d",&m);
for(i=0;i<m;i++)
{
fscanf(f,"%d",&caz);
if(caz==1)
{
++nh;
fscanf(f,"%ld",&H[nh].x);
H[nh].key=(++k);
poz[H[nh].key]=nh;
upheap(nh);
}
else if(caz==2)
{
fscanf(f,"%d",&y);
y=poz[y];
swap(y,nh);
--nh;
if(y/2&&H[y/2].x>H[y].x)
upheap(y);
else
downheap(y);
}
else
{
fprintf(g,"%ld\n",H[1].x);
}
}
}