Pagini recente » Profil pestcontrol485 | Cod sursa (job #360400)
Cod sursa(job #360400)
#include <fstream>
using namespace std;
#define nmax 200001
typedef struct { int key;long x; } heap;
heap H[nmax];
int k,nh,poz[nmax];
void swap(int a,int b)
{
heap c=H[a];
H[a]=H[b];
H[b]=c;
poz[H[a].key]=a;
poz[H[b].key]=b;
}
void upheap(int nod)
{
if(nod<=1) return;
int i=nod>>1;
if(H[nod].x<H[i].x)
{
swap(nod,i);
upheap(i);
}
}
void downheap(int nod)
{
int i=nod<<1;
if(i<=nh)
{
if(i+1<=nh&&H[i+1].x<H[i].x)
++i;
if(H[i].x<H[nod].x)
{
swap(i,nod);
downheap(i);
}
}
}
int main()
{
int m,i,caz,y;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>m;
for(i=0;i<m;i++)
{
f>>caz;
if(caz==1)
{
++nh;
f>>H[nh].x;
H[nh].key=(++k);
poz[H[nh].key]=nh;
upheap(nh);
}
else if(caz==2)
{
f>>y;
y=poz[y];
swap(y,nh);
--nh;
if(y/2&&H[y/2].x>H[y].x)
upheap(y);
else
downheap(y);
}
else
{
g<<H[1].x<<endl;
}
}
}