Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active May 1, 2016 19:24
Show Gist options
  • Save kartikkukreja/241ccb8758d625f1904f to your computer and use it in GitHub Desktop.
Save kartikkukreja/241ccb8758d625f1904f to your computer and use it in GitHub Desktop.
Range Updates and Point Queries with BIT
# 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