TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of [Segment Tree] (https://en.wikipedia.org/wiki/Segment_tree) data structure. More...
#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
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 | |
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. | |
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. | |
static void | test () |
Self-test implementation. | |
int | main () |
Main function. | |
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.
Definition in file segtree.cpp.
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 std::uint64_t 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) |
Definition at line 38 of file segtree.cpp.
int main | ( | void | ) |
Main function.
Definition at line 168 of file segtree.cpp.
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) |
Definition at line 63 of file segtree.cpp.
|
static |
Self-test implementation.
Definition at line 147 of file segtree.cpp.
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) |
Definition at line 103 of file segtree.cpp.