Pagini recente » Cod sursa (job #966398) | Cod sursa (job #1701860) | Cod sursa (job #95280) | Cod sursa (job #291919) | Cod sursa (job #405110)
Cod sursa(job #405110)
#include<fstream>
#define nmax 200001
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[nmax],n,m;
int poz_heap[nmax];
void insert_heap(int x) //insereaza nodul x in heap
{int fiu=++n; //fiu este nodul n+1 si incrementez n totodata
int tata=n/2;
while(heap[tata]>x)
//valoarea lui x trebuie promovata
{heap[fiu]=heap[tata];
fiu=tata;
tata=fiu/2;
}
heap[fiu]=x;
poz_heap[m]=fiu;
}
void stergere(int poz) //sterge nodul de pe poz poz din heap;
{int tata,fiu1,fiu2;
tata=poz;
fiu1=poz*2;
fiu2=poz*2+1;
heap[tata]=heap[n];
}
int main()
{int i,j,m,x,operatie,v;
fin>>m;
for(i=1;i<=m;i++)
{fin>>operatie;
switch(operatie)
{case 1 : fin>>x;
m++;
insert_heap(x);
break;
case 2 : fin>>x;
stergere(poz_heap[x]);
break;
case 3 : fout<<heap[1]<<'\n';
}
}
fin.close();
fout.close();
return 0;
}