Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikkukreja/87f25eca4be7d270d6e8 to your computer and use it in GitHub Desktop.
Save kartikkukreja/87f25eca4be7d270d6e8 to your computer and use it in GitHub Desktop.
Updating the segment tree
// We want to update the value associated with index in the input array
void update(int index, T value) {
update(1, 0, N-1, index, value);
}
// nodes[stIndex] is responsible for segment [lo, hi]
void update(int stIndex, int lo, int hi, int index, T value) {
if (lo == hi) {
nodes[stIndex].assignLeaf(value);
return;
}
int left = 2 * stIndex, right = left + 1, mid = (lo + hi) / 2;
if (index <= mid)
update(left, lo, mid, index, value);
else
update(right, mid+1, hi, index, value);
nodes[stIndex].merge(nodes[left], nodes[right]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment