Cod sursa(job #556782)
#include<fstream>
#define nmax 200100
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
long long a[nmax];
int heap[nmax], poz[nmax],n,k,aux;
void insert_heap(int i)
{
if(a[heap[i]]<a[heap[i/2]])
{
while(a[heap[i]]<a[heap[i/2]])
{
aux=heap[i/2];
heap[i/2]=heap[i];
heap[i]=aux;
poz[heap[i]]=i;
poz[heap[i/2]]=i/2;
i=i/2;
}
}
}
void reconstruct_heap(int x)
{
long long aux,l,r,min=x;
l=2*x;
r=2*x+1;
if(a[l]<a[r]&&l<=k)
min=l;
if(a[r]<a[l]&&r<=k)
min=r;
if(min!=x)
{
aux=heap[x];
heap[x]=heap[min];
heap[min]=aux;
aux=poz[heap[x]];
poz[heap[x]]=poz[heap[min]];
poz[heap[min]]=aux;
reconstruct_heap(min);
}
}
int main()
{
fin>>n;
long i,x;
int oper;
for(i=1;i<=n;i++)
{
fin>>oper;
if(oper==1)
{
k++;
fin>>a[k];
heap[k]=k;
poz[k]=k;
insert_heap(k);
}
if(oper==2)
{
fin>>x;
poz[heap[k]]=poz[x];
heap[poz[x]]=heap[k];
k--;
reconstruct_heap(poz[x]);
}
if(oper==3) fout<<a[heap[1]]<<'\n';
}
return 0;
}