Skip to content

Commit 4c88f61

Browse files
authored
Enhance walk function with optimized logic
Refactor walk function to improve performance and clarity.
1 parent f808141 commit 4c88f61

File tree

1 file changed

+12
-0
lines changed
  • library/data_structures_[l,r]/seg_tree_uncommon

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
int go(int l, int r) {
2+
//return __lg(min(l & -l, r - l + 1));
3+
//return __lg(min((l + n) & -(l + n), r - l + 1));
4+
return min(__builtin_ctz(l + n), __lg(r - l + 1));
5+
}
16
int walk(int l, int r, const auto& f) {
7+
/*
28
for (l += n, r += n; l <= r;)
39
if (int u = nxt(l, r); f(s[u])) {
410
while (u < n)
511
if (f(s[2 * u])) u *= 2;
612
else (u *= 2)++;
713
return u - n;
814
}
15+
*/
16+
while (l <= r) {
17+
int u = l + n, x = __lg(min(u & -u, r - l + 1));
18+
if (f(s[u >> x])) r = l + (1 << x) - 1;
19+
else l += 1 << x;
20+
}
921
return -1;
1022
}

0 commit comments

Comments
 (0)