Pagini recente » Cod sursa (job #423917) | Cod sursa (job #1538406) | Cod sursa (job #1526854) | Cod sursa (job #435299) | Cod sursa (job #3130956)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
#define nmax 200000
int heap[nmax],heapIN[nmax],m,auxIN[nmax],ind;
void showHeap()
{
out<<heap[1]<<endl;
}
void swap_in_heap(int x, int y)
{
int aux=heap[x];
heap[x]=heap[y];
heap[y]=aux;
aux=heapIN[x];
heapIN[x]=heapIN[y];
heapIN[y]=aux;
aux=auxIN[heapIN[x]];
auxIN[heapIN[x]]=auxIN[heapIN[y]];
auxIN[heapIN[y]]=aux;
}
void shiftOne(int k)
{
int child=1;
while (child)
{
child=0;
if (2*k <= m)
{
child= 2 * k;
if (2*k+1 <= m && heap[2 * k + 1] < heap[2 * k])
child= 2 * k + 1;
if (heap[child] > heap[k]) child=0;
}
if (child)
{
swap_in_heap(k, child);
k=child;
}
}
}
void sterge(int a)
{
heap[auxIN[a]]=heap[m];
m--;
shiftOne(auxIN[a]);
}
void percolate(int k)
{
while (k>1 && heap[k]<heap[k/2])
{
swap_in_heap(k, k / 2);
k=k/2;
}
}
void adauga(int a)
{
heap[++m]=a;
heapIN[m]=++ind;
auxIN[ind]=m;
percolate(m);
}
int main()
{
int n,op,x;
in >> n;
for(;n; n--)
{
in >> op;
if (op == 1)
{
in >> x;
adauga(x);
}
else
if (op == 2)
{
in >> x;
sterge(x);
}
else
showHeap();
}
return 0;
}