Pagini recente » Cod sursa (job #2123948) | Cod sursa (job #2716063) | Cod sursa (job #2947139) | Cod sursa (job #2038974) | Cod sursa (job #2218074)
#include <fstream>
#define NMAX 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[NMAX], a[NMAX], pos[NMAX], n;
int m, k, i, ok, dr, st, cod, x;
void inserare(int k)
{
while ( k/2 && a[heap[k]] < a[heap[k/2]] )
{
swap( a[heap[k]], a[heap[k/2]]);
pos[heap[k]] = k;
pos[heap[k/2]] = k/2;
k/=2;
}
}
void elimina(int k)
{
int ok = 1;
while ( ok != 0 )
{
ok = 0;
dr = k*2+1;
st = k*2;
if( st <= m )
{
if ( dr <= m && a[heap[dr]] < a[heap[st]] )
ok = dr;
else
ok = st;
if( a[heap[k]] > a[heap[ok]] )
{
swap(heap[k], heap[ok]);
pos[heap[k]] = k;
pos[heap[ok]] = ok;
k = ok;
}
else
ok = 0;
}
}
}
void solve()
{
f >> n;
for( i = 1 ; i <= n ; i++ )
{
f >> cod;
if ( cod == 3 )
g << a[heap[1]] << "\n";
else
{
f >> x;
if( cod == 1 )
{
k++ , m++ ;
a[k] = x;
heap[m] = k;
pos[k] = m;
inserare(k);
}
else
{
a[x] = -1;
inserare(pos[x]);
pos[heap[1]] = 0;
heap[1] = heap[m--];
pos[heap[1]] = 1;
if( m > 1 )
elimina(1);
}
}
}
}
int main()
{
solve();
return 0;
}