|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of [Segment Tree] (https://en.wikipedia.org/wiki/Segment_tree) data structure. More...
#include <cassert>#include <cmath>#include <iostream>#include <vector>Functions | |
| void | ConsTree (const std::vector< int64_t > &arr, std::vector< int64_t > *segtree, uint64_t low, uint64_t high, uint64_t pos) |
| for std::vector More... | |
| int64_t | query (std::vector< int64_t > *segtree, std::vector< int64_t > *lazy, uint64_t qlow, uint64_t qhigh, uint64_t low, uint64_t high, uint64_t pos) |
| Returns the sum of all elements in a range. More... | |
| void | update (std::vector< int64_t > *segtree, std::vector< int64_t > *lazy, int64_t start, int64_t end, int64_t delta, uint64_t low, uint64_t high, uint64_t pos) |
| Updates a range of the segment tree. More... | |
| static void | test () |
| Self-test implementation. More... | |
| int | main () |
| Main function. More... | |
Implementation of [Segment Tree] (https://en.wikipedia.org/wiki/Segment_tree) data structure.
A segment tree, also known as a statistic tree, is a tree data structure used for storing information about intervals, or segments. Its classical version allows querying which of the stored segments contain a given point, but our modification allows us to perform (query) any binary operation on any range in the array in O(logN) time. Here, we have used addition (+). For range updates, we have used lazy propagation.
| void ConsTree | ( | const std::vector< int64_t > & | arr, |
| std::vector< int64_t > * | segtree, | ||
| uint64_t | low, | ||
| uint64_t | high, | ||
| uint64_t | pos | ||
| ) |
for std::vector
for assert for log2 for IO operations
Constructs the initial segment tree
| arr | input to construct the tree out of |
| segtree | the segment tree |
| low | inclusive lowest index of arr to begin at |
| high | inclusive highest index of arr to end at |
| pos | index of segtree to fill (eg. root node) |
| int main | ( | void | ) |
Main function.
| int64_t query | ( | std::vector< int64_t > * | segtree, |
| std::vector< int64_t > * | lazy, | ||
| uint64_t | qlow, | ||
| uint64_t | qhigh, | ||
| uint64_t | low, | ||
| uint64_t | high, | ||
| uint64_t | pos | ||
| ) |
Returns the sum of all elements in a range.
| segtree | the segment tree |
| lazy | for lazy propagation |
| qlow | lower index of the required query |
| qhigh | higher index of the required query |
| low | lower index of query for this function call |
| high | higher index of query for this function call |
| pos | index of segtree to consider (eg. root node) |
|
static |
Self-test implementation.
| void update | ( | std::vector< int64_t > * | segtree, |
| std::vector< int64_t > * | lazy, | ||
| int64_t | start, | ||
| int64_t | end, | ||
| int64_t | delta, | ||
| uint64_t | low, | ||
| uint64_t | high, | ||
| uint64_t | pos | ||
| ) |
Updates a range of the segment tree.
| segtree | the segment tree |
| lazy | for lazy propagation |
| start | lower index of the required query |
| end | higher index of the required query |
| delta | integer to add to each element of the range |
| low | lower index of query for this function call |
| high | higher index of query for this function call |
| pos | index of segtree to consider (eg. root node) |