3 ms·
I want the hint to be available easily. i += i & -i adds the least-significant bit of i to i. This is not specific to Fenwick trees, it's a very common idio
by bugfix-66 4y ago
I want the hint to be available easily.
i += i & -i
adds the least-significant bit of i to i. This is not specific to Fenwick trees, it's a very common idiom.
Essentially, it doubles the size of the fragment to find the containing fragment. The addition can carry, for example 12+4 = 16 so the next larger fragment can be more than twice as large as the fragment we modified.
The idea is that these puzzles are very simple to fix. But you have to ponder the code (or research it elsewhere) to understand it enough to pinpoint the problem.
To fix the bug, simply change
sum := 0
to
sum := f.tree[0]
I think BUGFIX-66 may be too difficult for most people here on Hacker News. Even problem #1, which is almost trivial, is only solved by 2% of people who attempt it!