Pagini recente » Cod sursa (job #2478058) | Cod sursa (job #1077444) | Cod sursa (job #1697950) | Cod sursa (job #1052348) | Cod sursa (job #1763277)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N = 200003;
int n, h[N], a[N], nh, pos[N], l;
void schimba( int p, int q )
{
int aux = h[p];
h[p] = h[q];
h[q] = aux;
pos[ h[p] ] = p;
pos[ h[q] ] = q;
}
void coboara( int p )
{
int fs = 2*p, fd = 2*p+1, bun = p;
if ( fs <= nh && a[ h[fs] ] < a[ h[bun] ] )
bun = fs;
if ( fd <= nh && a[ h[fd] ] < a[ h[bun] ] )
bun = fd;
if ( bun != p )
{
schimba(bun, p);
coboara(bun);
}
}
void urca( int p )
{
while( p != 1 && a[ h[p] ] < a[ h[p/2] ] )
{
schimba(p, p/2);
p/=2;
}
}
void adauga( int val )
{
l++;
a[l] = val;
nh++;
h[nh] = l;
pos[ h[nh] ] = nh;
urca(nh);
}
void sterge( int p )
{
schimba(p, nh--);
urca(p);
coboara(p);
}
int main()
{
in >> n;
int i, x, nr;
for ( i = 1; i <= n; i++ )
{
in >> x;
if ( x == 1 )
{
in >> nr;
adauga(nr);
}
if ( x == 2 )
{
in >> nr;
sterge( pos[nr] );
}
if ( x == 3 )
out << a[ h[1] ] <<'\n';
}
return 0;
}