Refactoring and tidying up.

Segment tree and sparse table classes have been moved out into header files.
Profile code has been integrated into original code files via preprocessing flags.