Pagini recente » Cod sursa (job #1032854) | Cod sursa (job #1993356) | Statistici Horju Rares (HorjuRares) | Cod sursa (job #2064814) | Cod sursa (job #1148891)
#include <fstream>
using namespace std;
ifstream in( "heapuri.in" );
ofstream out( "heapuri.out" );
const int nmax = 200005;
int n, k, nr, nr1, x, operatie;
int heap[nmax], poz[nmax], v[nmax];
int aux;
void jos(int shp, int n)
{
int st = shp*2;
if ( st<=n )
{
if (st+1<=n && v[heap[st+1]] < v[heap[st]])
{
++st;
}
if (v[heap[st]]<v[heap[shp]])
{
aux = heap[st];
heap[st] = heap[shp];
heap[shp] = aux;
poz[heap[shp]] = shp;
poz[heap[st]] = st;
jos(st,n);
}
}
}
void update_heap(int shp)
{
int tata = shp/2;
if (tata>=1)
{
if ( v[heap[tata]]>v[heap[shp]] )
{
aux = heap[tata];
heap[tata] = heap[shp];
heap[shp] = aux;
poz[heap[shp]]= shp;
poz[heap[tata]]= tata;
update_heap(tata);
}
}
}
int main()
{
int player_unu=0;
in >> n;
for( int i= 1; i<=n; ++i )
{
in >> operatie;
if(operatie==1)
{
in>>x;
v[++nr1]= x;
poz[nr1]= nr;
heap[++nr]= nr1;
update_heap(nr);
}
if(operatie==2)
{
int y;
in >> x;
y=poz[x];
aux = heap[poz[x]];
heap[poz[x]] = heap[nr];
heap[nr] = aux;
poz[heap[nr]] = nr;
poz[heap[poz[x]]] = poz[x];
nr--;
jos(y,nr);
}
if(operatie==3)
{
out<<v[heap[1]]<<'\n';
}
}
return player_unu;
}