Supports up and down maintenance directions to facilitate manual maintenance of a single element in the heap (STL priority_queue and make_heap do not have this function t_t)
Namespace heap {
# Define p (x)> 1)
# Define L (x) <1)
# Define R (x) <1) + 1)
# Define maxn (INT) 1e5)
Template <typename T>
Struct heap {
T heap [maxn];
Int size;
Bool (* CMP) (T & A, T & B );
Void swap (T & A, T & B) {T = A; A = B; B = T ;}
Bool CMP (T & A, T & B) {return a <B ;}
Heap () {size = 0; CMP = CMP ;}
Heap (bool (* C) (T & A, T & B) {// The parameter is the pointer of the comparison function CMP.
Size = 0;
CMP = C;
}
Heap (T * begin, T * End ){
Size = end-begin;
Int I = 0;
For (; I <size; ++ I)
Heap [I + 1] = * (begin + I );
For (I = size; I; -- I)
Maintainup (I );
}
Void maintaindown (int K ){
Int M = K;
Bool CTN;
While (L (k) <size ){
CTN = 0;
If (CMP (heap [L (k)], heap [m]) M = L (k );
If (R (k) <size & CMP (heap [R (k)], heap [m]) M = R (k );
If (K! = M) Swap (M, k), cTn = 1, K = m;
If (! CTN) return;
}
}
Void maintainup (int K ){
While (P (k) & CMP (heap [K], heap [P (k)])
Swap (heap [K], heap [P (k)]), k = P (k );
}
Void insert (int K ){
Heap [++ size] = K;
Maintainup (k );
}
Void POP (int K ){
Heap [k] = heap [size --];
If (CMP (heap [P (k)], heap [k]) maintaindown (k );
Else maintainup (k );
}
};
} // Namespce heap
Heap (priority queue) template