Last active
August 29, 2015 14:09
-
-
Save kartikkukreja/87f25eca4be7d270d6e8 to your computer and use it in GitHub Desktop.
Updating the segment tree
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
// 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