TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Persistent segment tree with range updates (lazy propagation) More...
#include <iostream>
#include <memory>
#include <vector>
Go to the source code of this file.
Classes | |
class | range_queries::perSegTree |
Range query here is range sum, but the code can be modified to make different queries like range max or min. More... | |
class | range_queries::perSegTree::Node |
Namespaces | |
namespace | range_queries |
for std::vector | |
Functions | |
static void | test () |
Test implementations. | |
int | main () |
Main function. | |
Persistent segment tree with range updates (lazy propagation)
A normal segment tree facilitates making point updates and range queries in logarithmic time. Lazy propagation preserves the logarithmic time with range updates. So, a segment tree with lazy propagation enables doing range updates and range queries in logarithmic time, but it doesn't save any information about itself before the last update. A persistent data structure always preserves the previous version of itself when it is modified. That is, a new version of the segment tree is generated after every update. It saves all previous versions of itself (before every update) to facilitate doing range queries in any version. More memory is used ,but the logarithmic time is preserved because the new version points to the same nodes, that the previous version points to, that are not affected by the update. That is, only the nodes that are affected by the update and their ancestors are copied. The rest is copied using lazy propagation in the next queries. Thus preserving the logarithmic time because the number of nodes copied after any update is logarithmic.
Definition in file persistent_seg_tree_lazy_prop.cpp.
int main | ( | void | ) |
Main function.
Definition at line 318 of file persistent_seg_tree_lazy_prop.cpp.
|
static |
Test implementations.
Definition at line 268 of file persistent_seg_tree_lazy_prop.cpp.