1

Multiple segment trees vs. One big segment tree

 1 week ago
source link: https://codeforces.com/blog/entry/129191
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Multiple segment trees vs. One big segment tree

Sometimes you might need to use multiple segment trees over some set of elements, for example, you have to answer range queries on a single array from a lot of small arrays or to make a segment tree on a tree to answer path queries.

I knew that in cases like these, it might be more efficient to use a single segment tree to represent everything, for example, instead of building a segment tree on each one of the small arrays, you represent each array as a continuous range in one big segment tree.

I've used this whenever I needed an unknown/big number of segment trees, thinking that this is more efficient and uses less memory, but sometimes it wasn't the case as doing this has made my code slower, I once ended up doing a binary search on every query to find the actual borders of the queries, which I could've avoided by making a separate segment tree on each array.

I've thought about the advantages of using this technique, but I really couldn't find any.

I've found that, in both ways, I'm using the same amount of memory (4∑n=∑4n ), and both had the same speed.

So what I want to ask is, when does this technique give me real practical advantages to not use multiple segment trees in those cases? (other than HLD) and i forget Segment tree complexity is not only linear part n , but also log , and 4log(n)≠log(4n) although O() is the same of course. If you never query across arrays then in single big tree you'll have just wasted time and memory computing and storing aggregate over them its better than for memory


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK