题目
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int heap[N], sz, cnt;
int th[N], ht[N];
void hswap(int a, int b)
{
swap(heap[a], heap[b]);
swap(ht[a], ht[b]);
swap(th[ht[a]], th[ht[b]]);
}
void down(int h)
{
int t = h;
if(2*h <= sz && heap[2*h] < heap[t]) t = 2*h;
if(2*h+1 <= sz && heap[2*h+1] < heap[t]) t = 2*h+1;
if(t != h)
{
hswap(h, t);
down(t);
}
}
void up(int h)
{
int t = h/2;
if(t > 0 && heap[h] < heap[t])
{
hswap(h, t);
up(t);
}
}
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
string op;
cin >> op;
if(op == "I")
{
int x;
cin >> x;
heap[++sz] = x;
ht[sz] = ++cnt;
th[cnt] = sz;
up(sz);
}
else if(op == "PM")
{
cout << heap[1] << '\n';
}
else if(op == "DM")
{
hswap(1, sz--);
down(1);
}
else if(op == "D")
{
int k;
cin >> k;
int u = th[k]; //这一步要注意,交换之后有up,down操作,用参数就要存参数
hswap(th[k], sz--);
up(u);
down(u);
}
else if(op == "C")
{
int k, x;
cin >> k >> x;
heap[th[k]] = x;
up(th[k]);
down(th[k]);
}
}
}