模板—手写堆

struct Priority_queue
{
	int a[10000010],siz;
	void yh(int &s){if(s<siz&&a[s+1]>a[s])s++;}
	void dnn(int &x,int &s){swap(a[s],a[x]),x=s,s=x*2;}
	void up(int x){while(x>1)if(a[x/2]<a[x])swap(a[x],a[x/2]),x/=2;else break;}
	void dn(int x){int s=x*2;while(s<=siz){yh(s);if(a[s]>a[x])dnn(x,s);else break;}}
	void push(int x){a[++siz]=x;up(siz);}
	void pop(){a[1]=a[siz--];dn(1);}
	bool empty(){return !(bool)siz;}
	int top(){return a[1];}
}Q;

 

posted @ 2019-11-13 18:14  Al_Ca  阅读(239)  评论(0编辑  收藏  举报
ヾ(≧O≦)〃嗷~