Are you bored of writing duplicate codes when solving segment tree problems?
Did you find that segment trees have very little difference from each other?
Do you want a template, which allows you to code only the key parts, the parts different from other segment trees?
Then this template may help.
An example of segment add & multiply, segment query for sum modulo p:
codes
For more information (how to use it), please read README.md on Github.
http://www.cplusplus.com/doc/tutorial/classes/
Protip:
#define
and global-levelusing
shouldn't appear in headers. It restricts users of your code more than necessary. Defines can be completely replaced by inline functions or constexpr variables.You're also doing things like duplicating
initValues
into the class, even though you only use it once.The functions you pass as arguments can be template arguments.
Thanks for reminding me. I'm not good at programming except CP, so there may be lots of mistakes.
However, I used
#undef
at the end, and I think it may not restrict the users, am I right?Okay, I didn't notice the undef, which is better — it's not perfect since it would undef identically named macros in earlier includes if they're used later in the code. Define/ifdef are good for things like choosing pieces of code based on compatibility (e.g. system-specific constants) or specific purpose if one source can be used for several programs.
OK, I got it. And the issues mentioned by you are fixed now.
Just noticed that "duplicating
initValues
" means "duplicatinginit
function". I used to let it be a public function, and then realized it's useless and make it private.cool