#include<iostream> #include<algorithm> usingnamespace std; constint N = 100010;
// ph[k] 表示第 k 个插入的元素在堆数组中的下标 // hp[k] 表示堆数组 h 中第 k 个位置的元素是第几个插入的。
int h[N], ph[N], hp[N], cnt;
// 这里传入的是下标 voidheap_swap(int a, int b) { swap(ph[hp[a]], ph[hp[b]]); swap[hp[a], hp[b]]; swap(h[a], h[b]); }
voiddown(int u) { int t = u; if (u * 2 <= cnt && h[u * 2] < h[t]) t = u * 2; if (u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t]) t = u * 2 + 1; if (u != t) { // 交换下标即可 heap_swap(u, t); down(t); } }
voidup(int u) { while (u / 2 && h[u] < h[u / 2]) { heap_swap(u, u / 2); u /= 2; } }
intmain() { int n, m = 0; scanf("%d", &n); while (n -- ) { char op[5]; int k, x; scanf("%s", op); if (!strcmp(op, "I")) { scanf("%d", &x); cnt ++ ; m ++ ; ph[m] = cnt, hp[cnt] = m; h[cnt] = x; up(cnt); } elseif (!strcmp(op, "PM")) printf("%d\n", h[1]); elseif (!strcmp(op, "DM")) { heap_swap(1, cnt); cnt -- ; down(1); } elseif(!strcmp(op, "D")) { scanf("%d", &k); k = ph[k]; heap_swap(k, cnt); cnt -- ; up(k); down(k); } else { scanf("%d%d", &k, &x); k = ph[k]; h[k] = x; up(k); down(k); } } return0; }