Pagini recente » Profil UnseenMarksman | Cod sursa (job #1179517) | Cod sursa (job #2815827) | Cod sursa (job #1182876) | Cod sursa (job #1220760)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
#define cout g
int nr_operatii,tip_op,x,nr_int;//nr_int numarul de elemente intrate pana acum
int heap[200010],l_heap,nod[200010];//int nod[x] in ce nod e elementul ce a intrat al x-lea
//in heap ai indicii elementelor
int v[200010];//elementele propriu-zise
void sswitch(int & a,int &b)
{
int aux=a;
a=b;
b=aux;
}
void percolate(int nodd)
{
while (v[heap[nodd]]<v[heap[nodd/2]]&& nodd/2)
{
sswitch(heap[nodd],heap[nodd/2]);
nod[heap[nodd]]=nodd;
nod[heap[nodd/2]]=nodd/2;
nodd/=2;
}
}
void sift(int nodd)
{
if(v[heap[nodd]]>v[heap[nodd*2]] && nodd*2<=l_heap)
{
sswitch(heap[nodd],heap[nodd*2]);
nod[heap[nodd]]=nodd;
nod[heap[nodd*2]]=nodd*2;
nodd*=2;
sift(nodd);
}
else if (v[heap[nodd]]>v[heap[nodd*2+1]] && nodd*2+1<=l_heap)
{
sswitch(heap[nodd],heap[nodd*2+1]);
nod[heap[nodd]]=nodd;
nod[heap[nodd*2+1]]=nodd*2+1;
nodd=nodd*2+1;
sift(nodd);
}
}
int main()
{
f>>nr_operatii;
for(;nr_operatii;--nr_operatii)
{
f>>tip_op;
if(tip_op>2)
cout<<v[heap[1]]<<'\n';
else{
f>>x;
if(tip_op==1)
{
v[++nr_int]=x;
heap[++l_heap]=nr_int;
nod[nr_int]=l_heap;
percolate(l_heap);
}
else
{
heap[nod[x]]=heap[l_heap--];
nod[heap[nod[x]]]=nod[x];
percolate(nod[x]);
sift(nod[x]);
}
}
}
return 0;
}