跳到正文

剪贴板

剪贴板 r0jrk7j6

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
template <typename T>
class heap
{
private:
	vector<T> h;
	void rise(int x)
	{
		int t=x;
		while (h[t]<h[t/2])
		{
			swap(h[t],h[t/2]);
			t/=2;
		}
		return;
	}
	void sink(int x)
	{
		int t=x;
		while (t*2<h.size()&&h[t]>h[t*2]||t*2+1<h.size()&&h[t]>h[t*2+1])
		{
			if (t*2+1<h.size()&&h[t*2]>h[t*2+1])
			{
				swap(h[t],h[t*2+1]);
				t*=2;
				t++;
			}
			else
			{
				swap(h[t],h[t*2]);
				t*=2;
			}
		}
		return;
	}
public:
	heap():h(1){}
	void push(T x)
	{
		h.push_back(x);
		rise(h.size()-1);
		return;
	}
	void pop()
	{
		swap(h[h.size()-1],h[1]);
		h.pop_back();
		sink(1);
		return;
	}
	int top()
	{
		return h[1];
	}
};
heap<int> a;
int q;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>q;
	while (q--)
	{
		int op;
		cin>>op;
		if (op==1)
		{
			int x;
			cin>>x;
			a.push(x);
		}
		else if (op==2)
		{
			cout<<a.top()<<'\n';
		}
		else
		{
			a.pop();
		}
	}
	return 0;
}