1 int heap[maxn], n; 2 void heapPop() { 3 swap(heap[1], heap[n]); 4 -- n; 5 for (int i = 1, j; i <= n; i = j) { 6 if (i * 2 + 1 > n || heap[i * 2] < heap[i * 2 + 1]) { 7 j = i * 2; 8 } else { 9 j = i * 2 + 1; 10 } 11 if (heap[i] > heap[j]) { 12 swap(heap[i], heap[j]); 13 } else { 14 break; 15 } 16 } 17 } 18 void heapInsert(int x) { 19 heap[++ n] = x; 20 for (int i = n, j; i > 1; i = j) { 21 j = i / 2; 22 if (heap[i] < heap[j]) { 23 swap(heap[i], heap[j]); 24 } else { 25 break; 26 } 27 } 28 } 29 /* 30 1 31 / 32 2 3 33 / / 34 4 5 6 7 35 二分之一n(整除)一定是父节点 36 n的儿子是2n+1,2n+2(以0为根) 37 */