Pagini recente » Cod sursa (job #813660) | Cod sursa (job #97057) | Cod sursa (job #1809739) | Cod sursa (job #266574) | Cod sursa (job #2218077)
#include <fstream>
#define NMAX 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[NMAX], a[NMAX], pozitie[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(heap[k],heap[k/2]);
pozitie[heap[k]] = k;
pozitie[heap[k/2]] = k/2;
k /= 2;
}
}
void elimina(int k)
{
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]);
pozitie[heap[k]] = k;
pozitie[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;
pozitie[k] = m;
inserare(m);
}
else
{
a[x]=-1;
inserare(pozitie[x]);
pozitie[heap[1]] = 0;
heap[1] = heap[m--];
pozitie[heap[1]] = 1;
if( m > 1 )
elimina(1);
}
}
}
}
int main()
{
solve();
return 0;
}