跳到正文

剪贴板

剪贴板 evuiu1se

原文
剪贴板信息

剪贴板信息

收录
更新

二叉堆

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
struct node{
	int data;
}dui[N];
int cnt=0;
void push(int x){//添加元素
	cnt++;
	dui[cnt].data=x;//插入
	int f=cnt;
	while (f>1 && dui[f].data<dui[f/2].data){//上浮
		swap(dui[f],dui[f/2]);
		f/=2;
	}
}
void pop(){//删除元素
	dui[1].data=dui[cnt].data;//将堆底元素放入堆顶
	cnt--;//将堆底元素元素
	int f=1;
	while (2*f<=cnt){//下沉
		int son=2*f;
		if (son+1<=cnt && dui[son+1].data<dui[son].data){
			son++;
		}
		if (dui[son].data<dui[f].data){
			swap(dui[son],dui[f]);
			f=son;
		}
		else{
			break;
		}
	}
}