小根堆,求调
  • 板块学术版
  • 楼主whssy
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/7/25 10:37
  • 上次更新2023/11/3 07:47:12
查看原帖
小根堆,求调
809708
whssy楼主2023/7/25 10:37
#include<bits/stdc++.h>
using namespace std;
template<class T>
class tree_dot{
	public:
	T data;
	tree_dot<T> *lchild=NULL;
	tree_dot<T> *rchild=NULL;
	tree_dot<T> *father=NULL;
};
template<class T>
class heap_greater{
	public:
	long long cnt=0;
	tree_dot<T> *bt=NULL;
	bool empty(){
		if(cnt) return false;
		else return true;
	}
	T top(){
		return bt->data;
	}
	void pop(){
		--cnt;
		if(!cnt){
			bt=NULL;
			return;
		}
		tree_dot<T> *temp=bt;
		while(temp!=NULL){
			if(temp->lchild==NULL&&temp->rchild==NULL){
				if(temp->father->lchild==temp)
					temp->father->lchild=NULL;
				else
					temp->father->rchild=NULL;
				return;
			}else if(temp->lchild==NULL){
				temp->data=temp->rchild->data;
				temp=temp->rchild;
			}else if(temp->rchild==NULL){
				temp->data=temp->lchild->data;
				temp=temp->lchild;
			}else if(temp->lchild->data<temp->rchild->data){
				temp->data=temp->lchild->data;
				temp=temp->lchild;
			}else{
				temp->data=temp->rchild->data;
				temp=temp->rchild;
			}
		}
	}
	void push(T Indata){
		++cnt;
		if(cnt==1){
			bt=new tree_dot<T>;
			bt->data=Indata;
			bt->lchild=NULL;
			bt->rchild=NULL;
			bt->father=NULL;
			return;
		}
		long long temp___=cnt,cntmp=cnt;
		tree_dot<T>* temp=bt;
		int depth=-1;
		while(temp___){
			depth++;
			temp___>>=1;
		}
		if(cntmp&(1ll<<depth))
			cntmp^=1ll<<depth;
		depth--;
		while(depth){
			bool flag=cntmp>>depth&1;
			if(flag)
				cntmp^=1ll<<depth;
			if(flag)
				temp=temp->rchild;
			else
				temp=temp->lchild;
			depth--;
		}
		if(cntmp&1){
			temp->rchild=new tree_dot<T>;
			temp->rchild->lchild=NULL;
			temp->rchild->rchild=NULL;
			temp->rchild->data=Indata;
			temp->rchild->father=temp;
			temp=temp->rchild;
		}else{
			temp->lchild=new tree_dot<T>;
			temp->lchild->lchild=NULL;
			temp->lchild->rchild=NULL;
			temp->lchild->data=Indata;
			temp->lchild->father=temp;
			temp=temp->lchild;
		}
		while(temp->father!=NULL)
			if(temp->data<temp->father->data){
				swap(temp->data,temp->father->data);
				temp=temp->father;
			}else break;
	}
	long long size(){
		return cnt;
	}
};
int main(){
	int n;
	cin>>n;
	heap_greater<int>q;
	while(n--){
		int op;
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			q.push(x);
		}
		if(op==2)
			cout<<q.top()<<endl;
		if(op==3)
			q.pop();
	}
	return 0;
}
2023/7/25 10:37
加载中...