C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
struct heap {
elem_t h[MAXN]; int n, p, c; void init() { n = 0; } void ins(elem_t e) { for(p = ++n; p > 1 && _cp(e, h[p >> 1]); h[p] = h[p >> 1], p >>= 1); h[p] = e; } int del(elem_t &e) { if(!n) return 0; for(e = h[p = 1], c = 2; c < n && _cp(h[c += (c < n - 1 && _cp(h[c + 1], h[c]))], h[n]); h[p] = h[c], p = c, c <<= 1); h[p] = h[n--]; return 1; } }; |