Skip to content

Instantly share code, notes, and snippets.

@astiob
Created April 23, 2016 12:08
Show Gist options
  • Save astiob/6bc1956a01f130b63f80ee6b7a6d62cb to your computer and use it in GitHub Desktop.
Save astiob/6bc1956a01f130b63f80ee6b7a6d62cb to your computer and use it in GitHub Desktop.
Dynamic [i, n] range maximum query
T range_max(const T *binary_indexed_tree, size_t size, size_t first)
{
T value = T_MIN;
for (; first < size; first |= first + 1)
if (value < binary_indexed_tree[first])
value = binary_indexed_tree[first];
return value;
}
void increase(T *binary_indexed_tree, size_t pos, T value)
{
for (; pos != (size_t) -1; pos = (pos & pos + 1) - 1)
if (binary_indexed_tree[pos] < value)
binary_indexed_tree[pos] = value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment