Pagini recente » Cod sursa (job #2550245) | Cod sursa (job #38084) | Cod sursa (job #13767) | Cod sursa (job #2597295) | Cod sursa (job #2263298)
#include <bits/stdc++.h>
#define maxn 200000
using namespace std;
int nh = 0;
int hp[maxn+5]; /// min heap
int added[maxn+5];
map <int,int> adr;
void sch ( int pa, int pb )
{
swap ( adr[hp[pa]], adr[hp[pb]] );
swap ( hp[pa], hp[pb] );
}
void urca ( int x ) /// urca poz
{
while ( x > 1 && hp[x] < hp[x/2] )
{
sch ( x, x / 2 );
x /= 2;
}
}
void coboara ( int x ) /// coboara poz
{
int fs = 2 * x, fd = 2 * x + 1, bun = x;
if ( fs <= nh && hp[fs] < hp[bun] )
bun = fs;
if ( fd <= nh && hp[fd] < hp[bun] )
bun = fd;
if ( bun != x )
{
sch ( x, bun );
coboara ( bun );
}
}
void addnum ( int p ) /// adauga nr
{
hp[++nh] = p;
adr[p] = nh;
urca ( nh );
}
void delnum ( int p ) /// sterge poz
{
hp[p] = hp[nh--];
adr[p] = 0;
coboara ( p );
urca ( p );
}
int main ()
{
ifstream fin ( "heapuri.in" );
ofstream fout ( "heapuri.out" );
int t, op, p, ne = 0;
fin >> t;
for ( ; t > 0; t-- )
{
fin >> op;
if ( op == 1 )
{
fin >> p;
added[++ne] = p;
addnum ( p );
}
else if ( op == 2 )
{
fin >> p;
delnum ( adr[added[p]] );
}
else
fout << hp[1] << '\n';
}
fin.close ();
fout.close ();
return 0;
}