Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Last active November 5, 2020 16:29
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 sbsatter/d1f6d9fe528c9a9e23d22ea02119e1ba to your computer and use it in GitHub Desktop.
Save sbsatter/d1f6d9fe528c9a9e23d22ea02119e1ba to your computer and use it in GitHub Desktop.
Segment tree for sum of elements in an array
int n; // size of array
int arr[n]; // global array of elements that are provided as input
int tree[4 * n]; // global structure for containing segment tree
// initialize with global array arr
build(1, 1, n);
// query
query(1, 1, n, a, b); // query against range [a, b]
// update
update(1, 1, n, idx, val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment