Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:13
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/17f26686853e6053e7e2 to your computer and use it in GitHub Desktop.
Save kartikkukreja/17f26686853e6053e7e2 to your computer and use it in GitHub Desktop.
Range update
void update(int stIndex, int start, int end, UpdateType value) {
if (start == end) {
nodes[stIndex].applyUpdate(value);
return;
}
int mid = (nodes[stIndex].start + nodes[stIndex].end) / 2,
leftChildIndex = 2 * stIndex,
rightChildIndex = leftChildIndex + 1;
if (start > mid)
update(rightChildIndex, start, end, value);
else if (end <= mid)
update(leftChildIndex, start, end, value);
else {
update(leftChildIndex, start, mid, value);
update(rightChildIndex, mid+1, end, value);
}
nodes[stIndex].merge(nodes[leftChildIndex], nodes[rightChildIndex]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment