voidpush_down(vector<int> &heap, int size, int u){ int t = u, left = u * 2, right = u * 2 + 1; if (left <= size && heap[left] > heap[t]) t = left; if (right <= size && heap[right] > heap[t]) t = right; if (t != u) { swap(heap[u], heap[t]); push_down(heap, size, t); } }
voidpush_up(vector<int> &heap, int u){ while (u / 2 && heap[u / 2] < heap[u]) { swap(heap[u / 2], heap[u]); u /= 2; } }
voidheap_sort(vector<int> &q, int n){ int size = n; for (int i = 1; i<=n; i++) push_up(q, i);
for (int i = 1; i<=n; i++) { swap(q[1], q[size]); size --; push_down(q, size, 1); } }
voidinsert(vector<int> &heap, int size, int x){ heap[ ++ size] = x; push_up(heap, x); }