直接上代码:
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e6 + 1;
ll n, idx, h[N];
void up(ll u) {
while (u / 2 && h[u] < h[u / 2]) {
swap(h[u], h[u / 2]);
u /= 2;
}
}
void down(ll u) {
ll t = u;
if (u * 2 <= idx && h[u * 2] < h[t])
t = u * 2;
if (u * 2 + 1 <= idx && h[u * 2 + 1] < h[t])
t = u * 2 + 1;
if (t != u) {
swap(h[t], h[u]);
down(t);
}
}
ll top(ll h[]) {
return h[1];
}
void pop(ll h[]) {
h[1] = h[idx --];
down(1);
}
void insert(ll h[], ll x) {
h[++ idx] = x;
up(idx);
}
int main() {
ll q;
scanf("%lld", &q);
while (q --) {
ll op, x;
scanf("%lld", &op);
switch (op) {
case 1 : {
scanf("%lld", &x);
insert(h, x);
break;
}
case 2 : {
printf("%lld\n", top(h));
break;
}
default : {
pop(h);
break;
}
}
}
return 0;
}