From e4b572cae50d34008649f2ba138cc2595b92b2e3 Mon Sep 17 00:00:00 2001 From: Michael Hayter Date: Tue, 21 Nov 2023 03:18:59 -0500 Subject: [PATCH 1/2] Resolving issue #915 Attempt to resolve issue #915. Added additional explanation on how to use. Wondered if making arguments than don't change constant makes more sense? Formatted code more similarly to previous versions of segment tree. --- src/data_structures/segment_tree.md | 34 ++++++++++------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/data_structures/segment_tree.md b/src/data_structures/segment_tree.md index d9760bdd5..ab49fb990 100644 --- a/src/data_structures/segment_tree.md +++ b/src/data_structures/segment_tree.md @@ -385,30 +385,20 @@ However, this will lead to a $O(\log^2 n)$ solution. Instead, we can use the same idea as in the previous sections, and find the position by descending the tree: by moving each time to the left or the right, depending on the maximum value of the left child. -Thus finding the answer in $O(\log n)$ time. +Thus finding the answer in $O(\log n)$ time. +An example of using the following code would be get_first(1,0,n-1,5,8,14) since our segment tree starts at node 1. This would request a value greater than 14 between $[5,8]$. ```{.cpp file=segment_tree_first_greater} -int get_first(int v, int lv, int rv, int l, int r, int x) { - if(lv > r || rv < l) return -1; - if(l <= lv && rv <= r) { - if(t[v] <= x) return -1; - while(lv != rv) { - int mid = lv + (rv-lv)/2; - if(t[2*v] > x) { - v = 2*v; - rv = mid; - }else { - v = 2*v+1; - lv = mid+1; - } - } - return lv; - } - - int mid = lv + (rv-lv)/2; - int rs = get_first(2*v, lv, mid, l, r, x); - if(rs != -1) return rs; - return get_first(2*v+1, mid+1, rv, l ,r, x); +int get_first(int v, int tl, int tr, int l, int r, int greater_than) { + if(tl > r || tr < l) return -1; + if(t[v] <= greater_than) return -1; + + if (tl== tr) return tl; + + int tm = tl + (tr-tl)/2; + int left = get_first(2*v, tl, tm, l, r, greater_than); + if(left != -1) return left; + return get_first(2*v+1, tm+1, tr, l ,r, greater_than); } ``` From 0b8bc8d09a3c7c5d89a09a5ae47711210c7c2045 Mon Sep 17 00:00:00 2001 From: Jakob Kogler Date: Sun, 26 Nov 2023 15:57:27 +0100 Subject: [PATCH 2/2] Update segment_tree.md --- src/data_structures/segment_tree.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/data_structures/segment_tree.md b/src/data_structures/segment_tree.md index ab49fb990..5be8ec0a0 100644 --- a/src/data_structures/segment_tree.md +++ b/src/data_structures/segment_tree.md @@ -387,18 +387,17 @@ Instead, we can use the same idea as in the previous sections, and find the posi by moving each time to the left or the right, depending on the maximum value of the left child. Thus finding the answer in $O(\log n)$ time. -An example of using the following code would be get_first(1,0,n-1,5,8,14) since our segment tree starts at node 1. This would request a value greater than 14 between $[5,8]$. ```{.cpp file=segment_tree_first_greater} -int get_first(int v, int tl, int tr, int l, int r, int greater_than) { +int get_first(int v, int tl, int tr, int l, int r, int x) { if(tl > r || tr < l) return -1; - if(t[v] <= greater_than) return -1; + if(t[v] <= x) return -1; if (tl== tr) return tl; int tm = tl + (tr-tl)/2; - int left = get_first(2*v, tl, tm, l, r, greater_than); + int left = get_first(2*v, tl, tm, l, r, x); if(left != -1) return left; - return get_first(2*v+1, tm+1, tr, l ,r, greater_than); + return get_first(2*v+1, tm+1, tr, l ,r, x); } ``` 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