Pagini recente » Cod sursa (job #3214435) | Cod sursa (job #414816) | Cod sursa (job #3216283) | Cod sursa (job #301023) | Cod sursa (job #765711)
Cod sursa(job #765711)
#include <fstream>
#define NRMAX 300001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int hp[NRMAX],ap[NRMAX],a[NRMAX],n,nr,l;
void push(int);
void pop(int);
int main()
{
f>>n;a[0]=1000000000;
for (int i=1;i<=n;i++)
{
int tp,x,p;
f>>tp;
switch (tp)
{
case 1:
f>>x;
push(x);
break;
case 2:
f>>p;
pop(p);
break;
case 3:g<<a[hp[1]]<<'\n';
}
}
f.close();g.close();
return 0;
}
void push(int val)
{
nr++;l++;
a[nr]=val;
ap[nr]=l;
hp[l]=nr;
int poz=l;
while (a[hp[poz]]<a[hp[poz/2]] && poz/2>0)
{
int aux=hp[poz];
hp[poz]=hp[poz/2];
hp[poz/2]=aux;
int x=hp[poz],y=hp[poz/2];
aux=ap[x];ap[x]=ap[y];
ap[y]=aux;
poz=poz/2;
}
}
void pop(int poz)
{
hp[ap[poz]]=0;
int x=ap[poz];
ap[poz]=-1;
int y=0;
while (x!=y)
{
if (x*2<=l && a[hp[2*x]]<a[hp[y]]) y=x*2;
if (x*2+1<=l && a[hp[2*x+1]]<a[hp[y]])y=x*2+1;
ap[hp[x]]=y;
ap[hp[y]]=x;
int aux=hp[x];
hp[x]=hp[y];
hp[y]=aux;
x=y;
}
}