Pagini recente » Cod sursa (job #2647911) | Cod sursa (job #768323) | Cod sursa (job #2373675) | Cod sursa (job #787824) | Cod sursa (job #1183505)
#include <stdio.h>
#define N_MAX 200001
int heap[ N_MAX ], poz[ N_MAX ], v[ N_MAX ], ind = 1;
void schimb ( int p1, int p2 ){
int aux = heap[ p1 ];
heap[ p1 ] = heap[ p2 ];
heap[ p2 ] = aux;
poz[ heap[ p1 ] ] = p1;
poz[ heap[ p2 ] ] = p2;
return ;
}
void urc ( int x ){
if ( x == 1 ) return ;
if ( v[ heap[ x ] ] < v[ heap[ x / 2 ] ] ){
schimb ( x, x / 2 );
urc ( x / 2 );
}
return ;
}
void cobor ( int x ){
int a = 2 * x, b = 2 * x + 1;
if ( b < ind ){
if ( v[ heap[ b ] ] < v[ heap[ a ] ] && v[ heap[ b ] ] < v[ heap[ x ] ] ){
schimb ( x, b );
cobor ( b );
}
else if ( v[ heap[ a ] ] < v[ heap[ x ] ] ){
schimb ( x, a );
cobor ( a );
}
}
else if ( a < ind ) if ( v[ heap[ x ] ] > v[ heap[ a ] ] ){
schimb ( x, a );
cobor ( a );
}
return ;
}
int main()
{
FILE *in = fopen ( "heapuri.in", "r" );
FILE *out = fopen ( "heapuri.out", "w" );
int n;
fscanf ( in, "%d", &n );
int i, tip, x, aux;
for ( i = 1; i <= n; i++ ){
fscanf ( in, "%d", &tip );
if ( tip == 1 ){
fscanf ( in, "%d", &x );
v[ i ] = x;
heap[ ind ] = i;
poz[ i ] = ind;
ind++;
urc ( ind - 1 );
}
else if ( tip == 2 ){
fscanf ( in, "%d", &x );
aux = poz[ x ];
schimb ( aux, ind - 1 );
ind--;
if ( aux < ind ) cobor ( aux );
}
else fprintf ( out, "%d\n", v[ heap[ 1 ] ] );
}
fclose ( in );
fclose ( out );
return 0;
}