Pagini recente » Cod sursa (job #32537) | Cod sursa (job #1070459) | Cod sursa (job #1304396) | Cod sursa (job #1166197) | Cod sursa (job #2497097)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 200001;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
pair <int,int> Heap[NMAX];
int inh;
int pos[NMAX];
int h_size, N;
void HeapUp( int nod )
{
int parent = nod/2;
if( nod > 1 && Heap[parent] > Heap[nod] )
{
swap( Heap[parent], Heap[nod] );
swap( pos[Heap[parent].second], pos[Heap[nod].second] );
HeapUp( parent );
}
}
void HeapDown( int nod )
{
int son = 0;
int lf = nod * 2;
int rg = nod * 2 + 1;
do
{
son = 0;
if( lf <= h_size )
{
son = lf;
if( rg <= h_size && Heap[rg] < Heap[lf] )
son = rg;
if( Heap[nod] <= Heap[son] )
son = 0;
}
if( son )
{
swap( Heap[nod], Heap[son] );
swap( pos[Heap[nod].second], pos[Heap[son].second] );
}
}while( son );
}
void HeapAdd( pair<int,int> x )
{
Heap[++h_size] = x;
pos[x.second] = h_size;
HeapUp( h_size );
}
void HeapDel( int nod )
{
Heap[nod] = Heap[h_size--];
pos[Heap[h_size+1].second] = nod;
if( nod > 1 && Heap[nod] < Heap[nod/2] )HeapUp( nod );
else HeapDown( nod );
}
void Read()
{
fin >> N;
int op, x;
for( int i = 1; i <= N; ++i )
{
fin >> op;
if( op == 1 )
{
fin >> x;
inh++;
HeapAdd( {x,inh} );
}
if( op == 2 )
{
fin >> x;
HeapDel( pos[x] );
}
if( op == 3 )
fout << Heap[1].first << '\n';
}
}
int main()
{
Read();
return 0;
}