Pagini recente » Cod sursa (job #1429923) | Cod sursa (job #3312681) | Cod sursa (job #2880556) | Cod sursa (job #3236726) | Cod sursa (job #1220749)
#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 percolate(int nodd)
{
int x=v[heap[nodd]],aux=heap[nodd];
while (x<v[heap[nodd/2]])
{
heap[nodd]=heap[nodd/2];
nod[heap[nodd]]=nodd;
nodd/=2;
}
heap[nodd]=aux;
nod[aux]=nodd;
}
void sift(int nodd)
{
int x=v[heap[nodd]],aux=heap[nodd];
if(x>v[heap[nodd*2]] && nodd*2<=l_heap)
{
heap[nodd]=heap[nodd*2];
nod[heap[nodd]]=nodd;
nodd*=2;
}
else if (x>v[heap[nodd*2+1]] && nodd*2+1<=l_heap)
{
heap[nodd]=heap[nodd*2+1];
nod[heap[nodd]]=nod[heap[nodd*2+1]];
nodd=nodd*2+1;
}
heap[nodd]=aux;
nod[aux]=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;
}