#include <cstdio>
#include <algorithm>
using namespace std;
int top;
int heap[131073],pos[100001],
void pushup(int x)
{
while(x>1)
{
if(heap[x]<heap[x>>1])
swap(heap[x],heap[x>>1]),
swap(pos[heap[x]],pos[heap[x>>1]]),
x>>=1;
else
break;
}
return ;
}
void insert(int x)
{
heap[++top]=x;
pos[x]=top;
pushup(top);
return ;
}
void pop()
{
pos[heap[1]]=0;
heap[1]=heap[top--];
if(top)
pos[heap[1]]=1;
int x=2;
while(x<=top)
{
if(heap[x]>heap[x+1])
++x;
if(heap[x]<heap[x>>1])
swap(heap[x],heap[x>>1]),
swap(pos[heap[x]],pos[heap[x>>1]]),
x<<=1;
else
break;
}
return ;
}
最小堆 模版
最新推荐文章于 2023-12-28 21:03:24 发布