From 0a5336e17a7848d38e866348fb317494f9dcbed0 Mon Sep 17 00:00:00 2001 From: Anastasios Koutian Date: Wed, 20 Dec 2023 20:45:55 +0000 Subject: [PATCH] Add "build" function to segment_tree.md Previously, there was no function explaining how to build the tree for the case of range update/maximum query. --- src/data_structures/segment_tree.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/data_structures/segment_tree.md b/src/data_structures/segment_tree.md index 65516fa38..3e4948e8c 100644 --- a/src/data_structures/segment_tree.md +++ b/src/data_structures/segment_tree.md @@ -805,6 +805,17 @@ Before traversing to a child vertex, we call $\text{push}$ and propagate the val We have to do this in both the $\text{update}$ function and the $\text{query}$ function. ```cpp +void build(int a[], int v, int tl, int tr) { + if (tl == tr) { + t[v] = a[tl]; + } else { + int tm = (tl + tr) / 2; + build(a, v*2, tl, tm); + build(a, v*2+1, tm+1, tr); + t[v] = max(t[v*2], t[v*2 + 1]); + } +} + void push(int v) { t[v*2] += lazy[v]; lazy[v*2] += lazy[v]; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy