Skip to content

Commit e447c64

Browse files
lrvideckisweb-flow
andauthored
add seg tree walk (#179)
* add seg tree walk * [auto-verifier] verify commit b20b54c * update docs --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 0c0be65 commit e447c64

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
"tests/library_checker_aizu_tests/data_structures/rmq_sparse_table.test.cpp": "2026-01-18 11:15:41 +0000",
4545
"tests/library_checker_aizu_tests/data_structures/rmq_sparse_table_inc.test.cpp": "2026-01-18 11:15:41 +0000",
4646
"tests/library_checker_aizu_tests/data_structures/simple_tree.test.cpp": "2026-01-28 21:53:13 -0700",
47-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc.test.cpp": "2026-01-28 21:53:13 -0700",
48-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_line.test.cpp": "2026-01-28 21:53:13 -0700",
47+
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc.test.cpp": "2026-02-05 11:36:54 -0700",
48+
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_line.test.cpp": "2026-02-05 11:36:54 -0700",
49+
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp": "2026-02-05 11:36:54 -0700",
4950
"tests/library_checker_aizu_tests/data_structures/simple_tree_line.test.cpp": "2026-01-28 21:53:13 -0700",
5051
"tests/library_checker_aizu_tests/flow/dinic_aizu.test.cpp": "2024-11-17 14:04:03 -0600",
5152
"tests/library_checker_aizu_tests/flow/hungarian.test.cpp": "2024-11-17 14:04:03 -0600",

library/data_structures_[l,r]/seg_tree.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
//! https://codeforces.com/blog/entry/118682
33
//! @code
44
//! {
5-
//! tree st(n, 0LL, [&](ll l, ll r) {
6-
//! return l + r;
5+
//! tree st(a, [&](int vl, int vr) {
6+
//! return vl & vr;
77
//! });
88
//! }
9-
//! tree st(n, INT_MAX, ranges::min);
9+
//! {
10+
//! tree st(n, 0LL, plus<ll>{});
11+
//! }
12+
//! {
13+
//! tree st(n, INT_MAX, ranges::min);
14+
//! int idx = st.walk(l, r, [&](int value) {
15+
//! return value <= x;
16+
//! }); // smallest index in [l, r] s.t. f is true
17+
//! }
1018
//! @endcode
1119
//! @time O(n + q log n)
1220
//! @space O(n)
@@ -30,4 +38,5 @@ template<class T, class F> struct tree {
3038
while (l <= r) x = op(x, s[nxt(l, r)]);
3139
return x;
3240
}
41+
#include "seg_tree_uncommon/walk.hpp"
3342
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
T walk(int l, int r, function<bool(T)> f) {
2+
for (l += n, r += n; l <= r;)
3+
if (int u = nxt(l, r); f(s[u])) {
4+
while (u < n)
5+
if (f(s[2 * u])) u = 2 * u;
6+
else u = 2 * u + 1;
7+
return u - n;
8+
}
9+
return -1;
10+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#define PROBLEM \
2+
"https://judge.yosupo.jp/problem/predecessor_problem"
3+
#include "../template.hpp"
4+
#include "../../../library/data_structures_[l,r]/seg_tree.hpp"
5+
int main() {
6+
cin.tie(0)->sync_with_stdio(0);
7+
int n, q;
8+
cin >> n >> q;
9+
string s;
10+
cin >> s;
11+
vector<int> init(n);
12+
for (int i = 0; i < n; i++) init[i] = s[i] - '0';
13+
tree st(init, plus<int>{});
14+
while (q--) {
15+
int type, k;
16+
cin >> type >> k;
17+
if (type == 0) {
18+
if (st.query(k, k) == 0) st.update(k, 1);
19+
} else if (type == 1) {
20+
if (st.query(k, k) == 1) st.update(k, 0);
21+
} else if (type == 2) {
22+
cout << st.query(k, k) << '\n';
23+
} else if (type == 3) {
24+
// returns first element in [k,n-1] such that sum > 0
25+
cout << st.walk(k, n - 1, [&](int sum) {
26+
return sum > 0;
27+
}) << '\n';
28+
} else {
29+
assert(type == 4);
30+
int total = st.query(0, k);
31+
if (total == 0) {
32+
cout << -1 << '\n';
33+
} else {
34+
int pref_sum = 0;
35+
cout << st.walk(0, k, [&](int sum) {
36+
if (pref_sum + sum == total) return 1;
37+
pref_sum += sum;
38+
return 0;
39+
}) << '\n';
40+
}
41+
}
42+
}
43+
return 0;
44+
}

0 commit comments

Comments
 (0)