Блог пользователя ouuan

Автор ouuan, история, 5 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +86
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +44 Проголосовать: не нравится

Protip: #define and global-level using 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.

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    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?

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится +16 Проголосовать: не нравится

      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.

  • »
    »
    5 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

    Just noticed that "duplicating initValues" means "duplicating init function". I used to let it be a public function, and then realized it's useless and make it private.

»
4 года назад, # |
  Проголосовать: нравится -26 Проголосовать: не нравится

cool