1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <iostream> #include <algorithm> #include <string.h>
using namespace std;
const int N = 100010;
int n, m; int h[N], hp[N], ph[N], Size;
void heap_swap(int a, int b) { swap(ph[hp[a]], ph[hp[b]]); swap(hp[a],hp[b]); swap(h[a], h[b]); }
void down(int u) { int t = u; if(u * 2 <= Size && h[u * 2] < h[t]) t = u * 2; if(u * 2 + 1 <= Size && h[u * 2 + 1] < h[t]) t = u * 2 + 1; if(u != t) { heap_swap(t, u); down(t); } }
void up(int u) { while(u / 2 && h[u / 2] > h[u]) { heap_swap(u / 2, u); u /= 2; } }
int main() { scanf("%d", &n); while(n --) { char op[10]; int k, x; scanf("%s", op); if(!strcmp(op, "I")) { scanf("%d", &x); Size ++; m ++; ph[m] = Size, hp[Size] = m; h[Size] = x; up(Size); } else if(!strcmp(op, "PM")) printf("%d\n", h[1]); else if(!strcmp(op, "DM")) { heap_swap(1, Size); Size --; down(1); } else if(!strcmp(op, "D")) { scanf("%d", &k); k = ph[k]; heap_swap(k, Size); Size --; up(k), down(k); } else { scanf("%d%d", &k, &x); k = ph[k]; h[k] = x; up(k), down(k); } } return 0; }
|