插入:
inline void put(node x)
{
heap[++sz]=x.v;
int now=sz;
while(now>1&&heap[now>>1]>heap[now])
{
swap(heap[now],heap[now>>1]);
now>>=1;
}
}
原理比较简单,就是新加入一个点,然后在这个点所在链上维护小根堆的性质就好了。
删除(返回)堆顶:
inline void pop()
{
heap[1]=heap[sz--];
int x=1;
while (x<=(sz>>1))
{
int next=x<<1;
if (next<sz&&heap[next|1]<heap[next])next++;
if (heap[next]>heap[x])return;
swap(heap[next],heap[x]);
x=next;
}
}
也不难,就是先把最近一次插入的数放到堆顶,然后sz–(去掉一个数),然后再重新维护小根堆的性质,具体的话就是从上往下看是否有上面大于下面的然后交换就好了。