Pagini recente » Cod sursa (job #339956) | Cod sursa (job #612009) | Cod sursa (job #1791271) | Cod sursa (job #2068658) | Cod sursa (job #853931)
Cod sursa(job #853931)
#include <cstdio>
#include <cstdlib>
#define nmax 200001
#define mmax 100000001
FILE*f=fopen("heapuri.in","r");
FILE*g=fopen("heapuri.out","w");
int heap[nmax],poz[mmax],A[nmax];
int ind=0;
void swap(int &a,int &b)
{
int aux;
aux=a;
a=b;
b=aux;
}
void up_heap(int x)
{
if (x>1)
if (A[heap[x/2]]>A[heap[x]])
{
swap(heap[x/2],heap[x]);
swap(poz[heap[x/2]],poz[heap[x]]);
up_heap(x/2);
}
}
void down_heap(int x)
{
int m;
if (2*x<=ind)
{
m=2*x;
if (2*x+1<=ind && heap[2*x+1]<heap[2*x])
m=2*x+1;
if (A[heap[m]]<A[heap[x]])
{
swap(heap[m],heap[x]);
swap(poz[heap[m]],poz[heap[x]]);
down_heap(m);}
}
}
int main()
{
int n,tip,x;
fscanf(f,"%d",&n);
while (n>0)
{
fscanf(f,"%d",&tip);
if (tip==1)
{
fscanf(f,"%d",&x);
ind++;
heap[ind]=ind;
A[ind]=x;
poz[ind]=ind;
up_heap(ind);}
else
if (tip==2)
{
fscanf(f,"%d",&x);
A[x]=mmax;
down_heap(poz[x]);}
else
fprintf(g,"%d \n",A[heap[1]]);
n--;
}
return 0;
}