二差堆模板
蒟蒻zExNocs
·
·
个人记录
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000001;
int heap[maxn],heap_size=0;
void put(int d)
{
int now,next;
heap[++heap_size]=d;
now=heap_size;
while(now>1)
{
next=now>>1; //next=now/2
if(heap[now]>=heap[next]) break;
swap(heap[now],heap[next]);
now=next;
}
}
void del()
{
int now,next;
heap[1]=heap[heap_size--];
now=1;
while(now*2<=heap_size)
{
next=now*2;
if(next+1<=heap_size&&heap[next+1]<heap[next]) next++;
if(heap[now]<=heap[next]) break;
swap(heap[now],heap[next]);
now=next;
}
}
void out()
{
cout<<heap[1]<<endl;
}
int main()
{
int n,m,a;
cin>>n;
while(n--)
{
cin>>m;
if(m==1)
{
cin>>a;
put(a);
}
if(m==2) out();
if(m==3) del();
}
return 0;
}