Last active
May 1, 2016 19:24
-
-
Save kartikkukreja/241ccb8758d625f1904f to your computer and use it in GitHub Desktop.
Range Updates and Point Queries with BIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A[] is the original array | |
# ft[] is the fenwick tree maintaining the diffs initialized with 0 | |
# Add v to A[p] | |
update(p, v): | |
for (; p <= N; p += p&(-p)) | |
ft[p] += v | |
# Add v to A[a...b] | |
update(a, b, v): | |
update(a, v) | |
update(b + 1, -v) | |
# Return A[b] | |
query(b): | |
sum = 0 | |
for(; b > 0; b -= b&(-b)) | |
sum += ft[b] | |
return sum + A[b] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment