HN2new | past | comments | ask | show | jobs | submitlogin

Not sure how they got the 3N space overhead for Fenwick trees. If you do it right then Fenwick trees won't have any space overhead (in fact the conversion from an array to a Fenwick tree can be done in place). Though I suppose they might suffer from catastrophic cancellation if you're not careful.


I'm the author of that code. I have a semi-detailed writeup here: https://drive.google.com/file/d/17aK9ifP79abRuafXSD24jVVI3Sb...

Within one Fenwick tree we have two sorts of paths starting at any node, one going to a virtual node marked with 0, and one going to a node marked with positive infinity. The interesting property is that these two paths for any two starting nodes always intersect once.

In standard Fenwick trees you use one of those two paths to store data at the nodes visited during inserting, and retrieve that data using the other path at nodes visited during reading. This allows you to compute arbitrary prefix or suffix sums (depending on whether you use the 'up' or the 'down' path for insertion or reading).

However in the data structure I proposed we use both paths for storing and retrieving, allowing us to compute prefix and suffix sums. Finally to answer any sort of query we also need to store the original data in another array. Then any query for range [p, q] can be partitioned into three pieces: [p, m), {m}, (m, q] where m is the unique midpoint where the path going up from p and down from q intersect.


Standard Fenwick trees can only do prefix sums, which only get you general range queries on things with a subtraction operator, not operations like maximum.

The reddit comment I link contains an implementation that allegedly does arbitrary range queries, but it's nigh-incomprehensible so I can't tell how or why it uses 3 arrays.


I see, yeah I can't help you there either. I don't see how a tree based approach would ever need more than twice the amount of space.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: