Pagini recente » Cod sursa (job #1983919) | Cod sursa (job #620336) | Cod sursa (job #329217) | Cod sursa (job #97983) | Cod sursa (job #2732097)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("heapuri.in");
ofstream g ("heapuri.out");
int N,nr_elem,heap[200001],poz_heap[200001]; //elementul de pe poz i din heap a fost introdus al poz_heap[i]-lea
int poz[200001];//elementul al i-lea introdus e pe pozitia poz[i] in heap;
int father(int n)
{
return n/2;
}
int right_son(int n)
{
return n*2+1;
}
int left_son(int n)
{
return n*2;
}
int vmin()
{
return heap[1];
}
void sift(int nod)
{
int son;
while (true && left_son(nod)<=N)
{
son=left_son(nod);
if (right_son(nod)<=N && heap[son]>heap[right_son(nod)])
son=right_son(nod);
if (heap[son]<heap[nod])
{
swap(heap[son],heap[nod]);
swap(poz[poz_heap[son]],poz[poz_heap[nod]]);
swap(poz_heap[son],poz_heap[nod]);
nod=son;
}
else
break;
}
}
void percolate (int nod)
{
while (nod!=1 && heap[nod]<heap[father(nod)])
{
swap(heap[nod],heap[father(nod)]);
swap(poz[poz_heap[nod]],poz[poz_heap[father(nod)]]);
swap(poz_heap[nod],poz_heap[father(nod)]);
nod=father(nod);
}
}
void insert_nod(int val_nod)
{
heap[++N]=val_nod;
poz_heap[N]=++nr_elem;
poz[nr_elem]=N;
percolate(N);
}
void delete_nod(int nod)
{
heap[nod]=heap[N];
swap(poz[poz_heap[nod]],poz[poz_heap[N]]);
swap(poz_heap[nod],poz_heap[N--]);
if (father(nod) && heap[nod]<heap[father(nod)])
percolate(nod);
else
sift(nod);
}
void heapify(int arr[])
{
for (int i=N/2; i>0; --i)
sift(i);
}
int main()
{
g<<"";
int N,op,i,nr;
f>>N;
for (i=0; i<N; ++i)
{
f>>op;
if (op==1)
{
f>>nr;
insert_nod(nr);
}
else if (op==2)
{
f>>nr;
delete_nod(poz[nr]);
}
else
g<<vmin()<<"\n";
}
}