堆代码模板
作者:
impossible
,
2023-01-05 16:17:43
,
所有人可见
,
阅读 189
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,heap[N],m;
void up(int p){
while(p/2&&heap[p]>heap[p/2]){
swap(heap[p],heap[p/2]);
p/=2;
}
}
void inserts(int val){
heap[++n]=val;
up(n);
}
int getheaptop(){
return heap[1];
}
void down(int p){
int s=p*2;
while(s<=n){
if(s<n&&heap[s]<heap[s+1]) s++;
if(heap[s]>heap[p]){
swap(heap[s],heap[p]);
p=s;
s=p*2;
}else break;
}
}
void del(){
heap[1]=heap[n--];
down(1);
}
void delk(int k){
heap[k]=heap[n--];
up(k);
down(k);
}