TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
persistent_seg_tree_lazy_prop.cpp
Go to the documentation of this file.
1
25#include <iostream>
26#include <memory>
27#include <vector>
28
33namespace range_queries {
34
40 private:
41 class Node {
42 public:
43 std::shared_ptr<Node> left = nullptr;
44 std::shared_ptr<Node> right = nullptr;
45 int64_t val = 0,
46 prop = 0;
50 };
51
52 uint32_t n = 0;
53 std::vector<std::shared_ptr<Node>>
54 ptrs{};
57 std::vector<int64_t> vec{};
59
65 std::shared_ptr<Node> newKid(std::shared_ptr<Node> const &curr) {
66 auto newNode = std::make_shared<Node>(Node());
67 newNode->left = curr->left;
68 newNode->right = curr->right;
69 newNode->prop = curr->prop;
70 newNode->val = curr->val;
71 return newNode;
72 }
73
83 void lazy(const uint32_t &i, const uint32_t &j,
84 std::shared_ptr<Node> const &curr) {
85 if (!curr->prop) {
86 return;
87 }
88 curr->val += (j - i + 1) * curr->prop;
89 if (i != j) {
90 curr->left = newKid(curr->left);
91 curr->right = newKid(curr->right);
92 curr->left->prop += curr->prop;
93 curr->right->prop += curr->prop;
94 }
95 curr->prop = 0;
96 }
97
106 std::shared_ptr<Node> construct(const uint32_t &i, const uint32_t &j) {
107 auto newNode = std::make_shared<Node>(Node());
108 if (i == j) {
109 newNode->val = vec[i];
110 } else {
111 uint32_t mid = i + (j - i) / 2;
112 auto leftt = construct(i, mid);
113 auto right = construct(mid + 1, j);
114 newNode->val = leftt->val + right->val;
115 newNode->left = leftt;
116 newNode->right = right;
117 }
118 return newNode;
119 }
120
135 std::shared_ptr<Node> update(const uint32_t &i, const uint32_t &j,
136 const uint32_t &l, const uint32_t &r,
137 const int64_t &value,
138 std::shared_ptr<Node> const &curr) {
139 lazy(i, j, curr);
140 if (i >= l && j <= r) {
141 std::shared_ptr<Node> newNode = newKid(curr);
142 newNode->prop += value;
143 lazy(i, j, newNode);
144 return newNode;
145 }
146 if (i > r || j < l) {
147 return curr;
148 }
149 auto newNode = std::make_shared<Node>(Node());
150 uint32_t mid = i + (j - i) / 2;
151 newNode->left = update(i, mid, l, r, value, curr->left);
152 newNode->right = update(mid + 1, j, l, r, value, curr->right);
153 newNode->val = newNode->left->val + newNode->right->val;
154 return newNode;
155 }
156
171 int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l,
172 const uint32_t &r, std::shared_ptr<Node> const &curr) {
173 lazy(i, j, curr);
174 if (j < l || r < i) {
175 return 0;
176 }
177 if (i >= l && j <= r) {
178 return curr->val;
179 }
180 uint32_t mid = i + (j - i) / 2;
181 return query(i, mid, l, r, curr->left) +
182 query(mid + 1, j, l, r, curr->right);
183 }
184
189 public:
197 void construct(const std::vector<int64_t>
198 &vec) // the segment tree will be built from the values
199 // in "vec", "vec" is 0 indexed
200 {
201 if (vec.empty()) {
202 return;
203 }
204 n = vec.size();
205 this->vec = vec;
206 auto root = construct(0, n - 1);
207 ptrs.push_back(root);
208 }
209
219 void update(const uint32_t &l, const uint32_t &r,
220 const int64_t
221 &value) // all elements from index "l" to index "r" would
222 // by updated by "value", "l" and "r" are 0 indexed
223 {
224 ptrs.push_back(update(
225 0, n - 1, l, r, value,
226 ptrs[ptrs.size() -
227 1])); // saving the root pointer to the new segment tree
228 }
229
241 int64_t query(
242 const uint32_t &l, const uint32_t &r,
243 const uint32_t
244 &version) // querying the range from "l" to "r" in a segment tree
245 // after "version" updates, "l" and "r" are 0 indexed
246 {
247 return query(0, n - 1, l, r, ptrs[version]);
248 }
249
255 uint32_t size() // returns the number of segment trees (versions) , the
256 // number of updates done so far = returned value - 1
257 // ,because one of the trees is the original segment tree
258 {
259 return ptrs.size();
260 }
261};
262} // namespace range_queries
263
268static void test() {
269 std::vector<int64_t> arr = {-5, 2, 3, 11, -2, 7, 0, 1};
271 std::cout << "Elements before any updates are {";
272 for (uint32_t i = 0; i < arr.size(); ++i) {
273 std::cout << arr[i];
274 if (i != arr.size() - 1) {
275 std::cout << ",";
276 }
277 }
278 std::cout << "}\n";
279 tree.construct(
280 arr); // constructing the original segment tree (version = 0)
281 std::cout << "Querying range sum on version 0 from index 2 to 4 = 3+11-2 = "
282 << tree.query(2, 4, 0) << '\n';
283 std::cout
284 << "Subtract 7 from all elements from index 1 to index 5 inclusive\n";
285 tree.update(1, 5, -7); // subtracting 7 from index 1 to index 5
286 std::cout << "Elements of the segment tree whose version = 1 (after 1 "
287 "update) are {";
288 for (uint32_t i = 0; i < arr.size(); ++i) {
289 std::cout << tree.query(i, i, 1);
290 if (i != arr.size() - 1) {
291 std::cout << ",";
292 }
293 }
294 std::cout << "}\n";
295 std::cout << "Add 10 to all elements from index 0 to index 7 inclusive\n";
296 tree.update(0, 7, 10); // adding 10 to all elements
297 std::cout << "Elements of the segment tree whose version = 2 (after 2 "
298 "updates) are {";
299 for (uint32_t i = 0; i < arr.size(); ++i) {
300 std::cout << tree.query(i, i, 2);
301 if (i != arr.size() - 1) {
302 std::cout << ",";
303 }
304 }
305 std::cout << "}\n";
306 std::cout << "Number of segment trees (versions) now = " << tree.size()
307 << '\n';
308 std::cout << "Querying range sum on version 0 from index 3 to 5 = 11-2+7 = "
309 << tree.query(3, 5, 0) << '\n';
310 std::cout << "Querying range sum on version 1 from index 3 to 5 = 4-9+0 = "
311 << tree.query(3, 5, 1) << '\n';
312}
313
318int main() {
319 test(); // run self-test implementations
320 return 0;
321}
std::shared_ptr< Node > right
pointer to the left node
Range query here is range sum, but the code can be modified to make different queries like range max ...
std::shared_ptr< Node > newKid(std::shared_ptr< Node > const &curr)
Creating a new node with the same values of curr node.
uint32_t size()
Getting the number of versions after updates so far which is equal to the size of the pointers vector...
std::vector< std::shared_ptr< Node > > ptrs
number of elements/leaf nodes in the segment tree
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
void construct(const std::vector< int64_t > &vec)
Constructing the segment tree with the values in the passed vector. Returned root pointer is pushed i...
void lazy(const uint32_t &i, const uint32_t &j, std::shared_ptr< Node > const &curr)
If there is some value to be propagated to the passed node, value is added to the node and the childr...
int64_t query(const uint32_t &l, const uint32_t &r, const uint32_t &version)
Querying the range from index l to index r, getting the sum of the elements whose index x satisfies l...
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
void update(const uint32_t &l, const uint32_t &r, const int64_t &value)
Doing range update by passing the left and right indexes of the range as well as the value to be adde...
for std::vector
static void test()
Test implementations.
int main()
Main function.