Skip to content

Commit 289e733

Browse files
authored
Merge pull request cp-algorithms#1196 from mhayter/patch-8
Resolving issue cp-algorithms#915
2 parents 075c3c9 + 0b8bc8d commit 289e733

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/data_structures/segment_tree.md

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -385,30 +385,19 @@ However, this will lead to a $O(\log^2 n)$ solution.
385385
386386
Instead, we can use the same idea as in the previous sections, and find the position by descending the tree:
387387
by moving each time to the left or the right, depending on the maximum value of the left child.
388-
Thus finding the answer in $O(\log n)$ time.
388+
Thus finding the answer in $O(\log n)$ time.
389389
390390
```{.cpp file=segment_tree_first_greater}
391-
int get_first(int v, int lv, int rv, int l, int r, int x) {
392-
if(lv > r || rv < l) return -1;
393-
if(l <= lv && rv <= r) {
394-
if(t[v] <= x) return -1;
395-
while(lv != rv) {
396-
int mid = lv + (rv-lv)/2;
397-
if(t[2*v] > x) {
398-
v = 2*v;
399-
rv = mid;
400-
}else {
401-
v = 2*v+1;
402-
lv = mid+1;
403-
}
404-
}
405-
return lv;
406-
}
407-
408-
int mid = lv + (rv-lv)/2;
409-
int rs = get_first(2*v, lv, mid, l, r, x);
410-
if(rs != -1) return rs;
411-
return get_first(2*v+1, mid+1, rv, l ,r, x);
391+
int get_first(int v, int tl, int tr, int l, int r, int x) {
392+
if(tl > r || tr < l) return -1;
393+
if(t[v] <= x) return -1;
394+
395+
if (tl== tr) return tl;
396+
397+
int tm = tl + (tr-tl)/2;
398+
int left = get_first(2*v, tl, tm, l, r, x);
399+
if(left != -1) return left;
400+
return get_first(2*v+1, tm+1, tr, l ,r, x);
412401
}
413402
```
414403

0 commit comments

Comments
 (0)
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