Pagini recente » Cod sursa (job #2743079) | Cod sursa (job #2743025) | Cod sursa (job #1803668) | Istoria paginii utilizator/eduard240300 | Cod sursa (job #1006880)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
using namespace std;
class Heap{
private:
int* values;
int* heap;
int* pos;
int dimHeap; //dimensiunea heap-ului
int capVect;
int dim; //pentru inserarea in ord. cronologica in vectorul values
public:
Heap( int capVect ){
this->capVect = capVect;
dimHeap = 0;
dim = 0;
values = new int[this->capVect];
heap = new int[this->capVect];
pos = new int[this->capVect];
}
/*~Heap(){
delete[] values;
delete[] heap;
delete[] pos;
}*/
int parent( int i ){
return i/2;
}
int LeftSubTree( int i ){
return 2*i;
}
int RightSubTree( int i ){
return 2*i+1;
}
void pushUp( int poz ){
//int parent = poz / 2;
while( poz/2 > 1 && values[heap[poz/2]] > values[heap[poz]] ){
swap( heap[poz/2], heap[poz] );
//swap( pos[heap[parent]], pos[heap[poz]] )
pos[heap[poz]] = poz;
pos[heap[poz/2]] = poz/2;
poz /= 2;
//parent = poz / 2;
}
}
void pushDown( int poz ){
int y = 0;
while ( poz != y ){
y = poz;
if (y*2<=dimHeap && values[heap[poz]]> values[heap[y*2]]) poz = y*2;
if (y*2+1<=dimHeap && values[heap[poz]]> values[heap[y*2+1]]) poz = y*2+1;
swap( heap[poz], heap[y] );
pos[heap[poz]] = poz;
pos[heap[y]] = y;
}
}
void insert( int x ){
dimHeap++;
dim++;
values[dim] = x;
heap[dimHeap] = dim;
pos[dim] = dimHeap;
pushUp(dimHeap);
}
int peek(){
return values[heap[1]];
}
void delete1( int i ){
values[i] = -1;
assert( pos[i] != 0 );
assert( 1 <= i && i <= dim );
pushUp( pos[i] );
pos[heap[1]]=0;
heap[1] = heap[dimHeap--];
pos[heap[1]] = 1;
if( dimHeap > 1 ) pushDown(1);
}
};
int main(){
Heap h(200010);
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int cod, x, N;
scanf("%d", &N);
for( int i = 1; i <= N; i++ ){
scanf("%d", &cod );
switch( cod ){
case 1 :
scanf("%d", &x );
h.insert(x);
break;
case 2 :
scanf("%d", &x );
h.delete1(x);
break;
case 3 :
printf("%d\n",h.peek() );
break;
default:
break;
}
}
return 0;
}