Pagini recente » Cod sursa (job #559231) | Cod sursa (job #2142573) | Cod sursa (job #529120) | Cod sursa (job #1445354) | Cod sursa (job #842454)
Cod sursa(job #842454)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,m,auxiliar,heap[200005],minim=0,poz[200005],inc[2000005],x;
int pozitie=0;
void swap(int p1,int p2)
{
int auxiliar = heap[p1];
heap[p1] = heap[p2];
heap[p2] = auxiliar;
auxiliar = poz[inc[p1]];
poz[inc[p1]] = poz[inc[p2]];
poz[inc[p2]] = auxiliar;
auxiliar = inc[p1];
inc[p1] = inc[p2];
inc[p2] = auxiliar;
}
void sift(int poz,int q)
{
int son;
do{
son = 0;
if(2*poz <= q)
{
son = 2*poz;
if(heap[2*poz+1] <= q && heap[2*poz]<heap[2*poz+1])
son = 2*poz + 1;
if(heap[son]>=heap[poz])
son =0;
}
if(son)
swap(poz,son);
}while(son);
}
void percolate(int q)
{
while(q>1 && heap[q]<heap[q/2])
swap(q,q/2),q = q/2;
}
int main()
{
f>>n;
while(n--)
{
f>>auxiliar;
switch(auxiliar)
{
case 1:
{
f>>x;
heap[++m]=x;
poz[m] = m;
inc[m] = m;
percolate(m);
break;
}
case 2:
{
f>>x;
pozitie = poz[x];
swap(pozitie,m);
if(heap[pozitie]<heap[pozitie/2])
percolate(pozitie);
else
sift(pozitie,m-1);
m--;
break;
}
case 3:
{
g<<heap[1]<<'\n';
break;
}
}
}
}