Boring_Day's blog

By Boring_Day, history, 20 months ago, In English

If it is possible to create a vector of class then it should not be an issue to create vector of segment tree in c++.

Can you help me in these two things.

1st) when the size of segment tree at each index is same.

2nd) when the sizes at different indices are different.

Even if you help me solve the 1st thing i will be thankful to you.

  • Vote: I like it
  • -10
  • Vote: I do not like it

| Write comment?
»
20 months ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

It seems that you just want a segment tree encapsulated by a class like this

Spoiler
  • »
    »
    20 months ago, # ^ |
    Rev. 5   Vote: I like it 0 Vote: I do not like it

    Thank you so much my friend. I was getting run time error because i was using parameterized constructor instead of init function.

    Can we use parameterized constructor somehow?

    • »
      »
      »
      20 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you send your code so I can help you debug

    • »
      »
      »
      20 months ago, # ^ |
      Rev. 2   Vote: I like it -13 Vote: I do not like it
      struct SegtreeOrWhatever {
          int val;
      
          SegtreeOrWhatever(int x, int y) : val(x + y) {}
      
          int func() { return val; }
      };
      
      int main() {
      
          std::vector<SegtreeOrWhatever> vec;
          vec.reserve(2);
      
          vec.emplace_back(2, 3);
          vec.emplace_back(42, 69);
      
          std::cout << vec[0].func() << ' ' << vec[1].func();
      
      }
      

      Edit: check talks

»
20 months ago, # |
  Vote: I like it +1 Vote: I do not like it

You can use this. It'll work out of the box for same size segment trees. If you want to add variable size segment trees to it just give the class a default constructor and emplace_back the segtrees as required.

auto merge = [&](int a, int b) { return a+b; };
using ST = Segtree<int, decltype(merge)>;
vector<ST> arr(n, ST(n, 0, merge));
// or arr.emplace_back(n, 0, merge);