Pagini recente » Cod sursa (job #1043908) | Cod sursa (job #1077550) | Cod sursa (job #1910897) | Cod sursa (job #1126628) | Cod sursa (job #989934)
Cod sursa(job #989934)
#include<fstream>
#include<list>
#define dmax 200003
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n;
int heap[dmax];
int length = -1;
int hp[dmax];
int ip[dmax];
void hSwap(int p1, int p2)
{
int t;
t = heap[p1];
heap[p1] = heap[p2];
heap[p2] = t;
int i1 = hp[p1];
int i2 = hp[p2];
t = hp[p1];
hp[p1] = hp[p2];
hp[p2] = t;
t = ip[i1];
ip[i1] = ip[i2];
ip[i2] = t;
}
void up(int pos)
{
if(pos <= 0)
return;
int father = pos/2;
if(heap[father] > heap[pos])
{
hSwap(pos, father);
up(father);
}
}
void down(int pos)
{
if(pos > length/2)
return;
int left, right;
left = 2*pos;
right = 2*pos+1;
int mn = pos;
if(left <= length && heap[left] < heap[pos])
mn = left;
else if(right <= length && heap[right] < heap[mn])
mn = right;
if(mn != pos)
{
int t;
hSwap(mn, pos);
down(mn);
}
}
void hInsert(int k)
{
length++;
heap[length] = k;
hp[length] = length;
ip[length] = length;
up(length);
}
void getMin()
{
out<<heap[0]<<'\n';
}
void hRemove(int k)
{
int pos = ip[k];
hSwap(pos, length);
length--;
down(pos);
}
int main()
{
in>>n;
for(int i=1; i<=n; i++)
{
int op;
in>>op;
if(op == 1)
{
int a;
in>>a;
hInsert(a);
}
else if(op == 2)
{
int a;
in>>a;
hRemove(a-1);
}
else getMin();
/*
for(int i=0; i<=length; i++)
out<<heap[i]<<" ";
out<<'\n';
*/
}
in.close();
out.close();
return 0;
}