Pagini recente » Cod sursa (job #1002383) | Cod sursa (job #2872233) | Cod sursa (job #1078520) | Cod sursa (job #2199955) | Cod sursa (job #2670310)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,x,t,v[200005],heap[200005],poz[200005],nr,tr;
void adun(int k)
{
while( k/2 && v[heap[k]]<v[heap[k/2]] )
{
swap(heap[k],heap[k/2]);
swap(poz[heap[k]],poz[heap[k/2]]);
x/=2;
}
}
void scad(int k)
{
while( k/2 && v[heap[k]]<v[heap[k/2]] )
{
swap(heap[k],heap[k/2]);
swap(poz[heap[k]],poz[heap[k/2]]);
x/=2;
}
int y;
while( (k*2<=tr && v[heap[k]]>v[heap[k*2]]) || (k*2+1<=tr && v[heap[k]]>v[heap[k*2+1]]) )
{
y=-1;
if( k*2<=tr && v[heap[k]]>v[heap[k*2]] ) y=2*k;
if( k*2+1<=tr && v[heap[k]]>v[heap[k*2+1]] ){
if( y==-1 ) y=2*k+1;
else if( v[heap[k*2+1]]<v[heap[k*2]] ) y=2*k+1;
}
swap(heap[k],heap[y]);
swap(poz[heap[k]],poz[heap[y]]);
x=y;
}
}
int main()
{
f>>t;
while(t--)
{
f>>n;
if(n<3) f>>x;
if( n==1 )
{
nr++; tr++;
v[nr]=x;
heap[tr]=nr;
poz[nr]=tr;
adun(tr);
}
if( n==2 )
{
heap[poz[x]]=heap[tr];
poz[heap[tr]]=poz[x];
tr--;
scad(poz[x]);
}
if( n==3 )
{
g<<v[heap[1]]<<'\n';
}
}
}