Pagini recente » Cod sursa (job #586109) | Cod sursa (job #2893021) | Cod sursa (job #3127354) | Cod sursa (job #2118561) | Cod sursa (job #2809253)
#include <stdio.h>
#include <iostream>
#define MAXN 200000
inline int parent( int ind ) { return (ind - 1) / 2; }
inline int left_child( int ind ) { return ind * 2 + 1; }
inline int right_child( int ind ) { return ind * 2 + 2; }
int heap[MAXN];
int cron[MAXN], heaptocron[MAXN];
int heap_size, n_cr;
void swap( int nod1, int nod2 ) {
int c1, c2;
std::swap( heap[nod1], heap[nod2] );
c1 = heaptocron[nod1];
c2 = heaptocron[nod2];
std::swap( heaptocron[nod1], heaptocron[nod2] );
std::swap( cron[c1], cron[c2] );
}
void upHeap( int nod ) {
if( nod > 0 && heap[nod] < heap[parent(nod)] ) {
swap( nod, parent(nod) );
upHeap( parent(nod) );
}
}
void downHeap( int nod ) {
int nod_min;
nod_min = left_child(nod);
if( right_child(nod) < heap_size && heap[right_child(nod)] < heap[nod_min] )
nod_min = right_child(nod);
if( nod_min < heap_size && heap[nod] > heap[nod_min] ) {
swap( nod, nod_min );
downHeap( nod_min );
}
}
void insertVal( int x ) {
heap[heap_size] = x;
heaptocron[heap_size] = n_cr;
cron[n_cr++] = heap_size;
upHeap( heap_size++ );
}
void deleteVal( int nod ) {
swap( nod, --heap_size );
downHeap(nod);
upHeap(nod);
}
int getMin() {
return heap[0];
}
int main() {
FILE *fin, *fout;
int n, i, op, x;
fin = fopen( "heapuri.in", "r" );
fout = fopen( "heapuri.out", "w" );
fscanf( fin, "%d", &n );
for( i = 0; i < n; i++ ) {
fscanf( fin, "%d", &op );
switch( op ) {
case 1:
fscanf( fin, "%d", &x );
insertVal(x);
break;
case 2:
fscanf( fin, "%d", &x );
deleteVal( cron[x - 1] );
break;
case 3:
fprintf( fout, "%d\n", getMin() );
break;
}
}
fclose( fin );
fclose( fout );
return 0;
}