Pagini recente » Cod sursa (job #251155) | Cod sursa (job #3122934) | Cod sursa (job #2893541) | Cod sursa (job #3229639) | Cod sursa (job #2809784)
#include <stdio.h>
#define NMAXX 200000
using namespace std;
int heap[NMAXX], poz[NMAXX];
bool v[NMAXX];
int heapSize, ordine;
int parent( int i ) {
return (i - 1) / 2;
}
int leftChild( int i ) {
return i * 2 + 1;
}
int rightChild( int i ) {
return i * 2 + 2;
}
void swap( int &a, int &b ) {
int aux;
aux = b;
b = a;
a = aux;
}
void upHeap( int i ) {
while( i > 0 && heap[parent( i )] > heap[i] ) {
swap( heap[parent( i )], heap[i] );
swap( poz[parent( i )], poz[i] );
i = parent( i );
}
}
void downHeap( int i ) {
int left, right, smallest;
smallest = i;
left = leftChild( i );
right = rightChild( i );
if ( left < heapSize && heap[left] < heap[smallest] )
smallest = left;
if ( right < heapSize && heap[right] < heap[smallest] )
smallest = right;
if ( i != smallest ) {
swap( heap[i], heap[smallest] );
swap( poz[i], poz[smallest] );
downHeap( smallest );
}
}
void insert( int val ) {
heap[heapSize] = val;
poz[heapSize++] = ordine++;
upHeap( heapSize - 1 );
}
int exctractMin() {
int minn;
minn = heap[0];
heap[0] = heap[--heapSize];
poz[0] = poz[heapSize];
downHeap( 0 );
return minn;
}
void update( int i, int val ) {
heap[i] = val;
upHeap( i );
downHeap( i );
}
int main()
{
FILE *fin, *fout;
int n, cer, i, a;
fin = fopen( "heapuri.in", "r" );
fout = fopen( "heapuri.out", "w" );
fscanf( fin, "%d", &n );
for ( i = 0; i < n; i++ ) {
fscanf( fin, "%d", &cer );
if ( cer == 1 ) {
fscanf( fin, "%d", &a );
insert( a );
} else if ( cer == 2 ) {
fscanf( fin, "%d", &a );
v[a - 1] = 1;
} else {
while ( v[poz[0]] == 1 )
exctractMin();
fprintf( fout, "%d\n", heap[0] );
}
}
fclose( fin );
fclose( fout );
return 0;
}