Skip to content

Commit 9e9d341

Browse files
committed
another idea
1 parent 2ea0b33 commit 9e9d341

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
int walk(int l, int r, const auto& f) {
2-
while (l <= r) {
3-
int u = l + n, v = __lg(min(u & -u, r - l + 1));
4-
if (f(s[u >> v])) l += 1 << v;
5-
else r = l + (1 << v) - 2;
6-
}
7-
return l;
2+
l += n, r += n;
3+
if (T x = s[l]; f(x))
4+
for (l++; l <= r;) {
5+
int v = __lg(min(l & -l, r - l + 1));
6+
if (T y = op(x, s[l >> v]); f(y)) l += 1 << v, x = y;
7+
else r = l + (1 << v) - 2;
8+
}
9+
return l - n;
810
}

tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/predecessor_problem"
33
#include "../template.hpp"
4+
int __lg(unsigned x) {
5+
return bit_width(x) - 1;
6+
}
47
#include "../../../library/data_structures_[l,r]/seg_tree.hpp"
58
int main() {
69
cin.tie(0)->sync_with_stdio(0);
@@ -32,13 +35,8 @@ int main() {
3235
if (total == 0) {
3336
cout << -1 << '\n';
3437
} else {
35-
int pref_sum = 0;
3638
cout << st.walk(0, k, [&](int sum) {
37-
if (pref_sum + sum < total) {
38-
pref_sum += sum;
39-
return 1;
40-
}
41-
return 0;
39+
return sum < total;
4240
}) << '\n';
4341
}
4442
}

0 commit comments

Comments
 (0)