constint N = 150100; boolmy_greater(int x, int y){ return x > y; } boolmy_less(int x, int y){ return x < y; } structHeap { int n; int a[N]; bool (*cmp)(int, int); Heap(bool (*_cmp)(int, int)) : cmp(_cmp) {} ;
voidpercolateUp(int u){ while (u > 1 && cmp(a[u], a[u >> 1])) swap(a[u], a[u >> 1]), u >>= 1; }
voidpercolateDown(int u){ for (int v = u << 1; v <= n; u = v, v = u << 1) { if (v + 1 <= n && cmp(a[v + 1], a[v])) v++; if (!cmp(a[v], a[u])) break; swap(a[v], a[u]); } }
voidinsert(int x){ a[++n] = x; percolateUp(n); }
voidpop(){ a[1] = a[n--]; percolateDown(1); }
voidadd(int x){ a[++n] = x; } voidbuild(){ for (int i = n >> 1; i >= 1; i--) percolateDown(i); }