From b20b54ce20d6bcaeb7ac2e52b91e868fe6e62f67 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Thu, 5 Feb 2026 11:36:54 -0700 Subject: [PATCH 1/3] add seg tree walk --- library/data_structures_[l,r]/seg_tree.hpp | 1 + .../seg_tree_uncommon/walk.hpp | 10 +++++ .../simple_tree_inc_walk.test.cpp | 44 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 library/data_structures_[l,r]/seg_tree_uncommon/walk.hpp create mode 100644 tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp diff --git a/library/data_structures_[l,r]/seg_tree.hpp b/library/data_structures_[l,r]/seg_tree.hpp index d2726674..3cc7ee57 100644 --- a/library/data_structures_[l,r]/seg_tree.hpp +++ b/library/data_structures_[l,r]/seg_tree.hpp @@ -30,4 +30,5 @@ template struct tree { while (l <= r) x = op(x, s[nxt(l, r)]); return x; } +#include "seg_tree_uncommon/walk.hpp" }; diff --git a/library/data_structures_[l,r]/seg_tree_uncommon/walk.hpp b/library/data_structures_[l,r]/seg_tree_uncommon/walk.hpp new file mode 100644 index 00000000..58ad084e --- /dev/null +++ b/library/data_structures_[l,r]/seg_tree_uncommon/walk.hpp @@ -0,0 +1,10 @@ +T walk(int l, int r, function f) { + for (l += n, r += n; l <= r;) + if (int u = nxt(l, r); f(s[u])) { + while (u < n) + if (f(s[2 * u])) u = 2 * u; + else u = 2 * u + 1; + return u - n; + } + return -1; +} diff --git a/tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp b/tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp new file mode 100644 index 00000000..1dd9fcf9 --- /dev/null +++ b/tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp @@ -0,0 +1,44 @@ +#define PROBLEM \ + "https://judge.yosupo.jp/problem/predecessor_problem" +#include "../template.hpp" +#include "../../../library/data_structures_[l,r]/seg_tree.hpp" +int main() { + cin.tie(0)->sync_with_stdio(0); + int n, q; + cin >> n >> q; + string s; + cin >> s; + vector init(n); + for (int i = 0; i < n; i++) init[i] = s[i] - '0'; + tree st(init, plus{}); + while (q--) { + int type, k; + cin >> type >> k; + if (type == 0) { + if (st.query(k, k) == 0) st.update(k, 1); + } else if (type == 1) { + if (st.query(k, k) == 1) st.update(k, 0); + } else if (type == 2) { + cout << st.query(k, k) << '\n'; + } else if (type == 3) { + // returns first element in [k,n-1] such that sum > 0 + cout << st.walk(k, n - 1, [&](int sum) { + return sum > 0; + }) << '\n'; + } else { + assert(type == 4); + int total = st.query(0, k); + if (total == 0) { + cout << -1 << '\n'; + } else { + int pref_sum = 0; + cout << st.walk(0, k, [&](int sum) { + if (pref_sum + sum == total) return 1; + pref_sum += sum; + return 0; + }) << '\n'; + } + } + } + return 0; +} From 9f5eb562606062a9e88314c7e97206113a05eaa5 Mon Sep 17 00:00:00 2001 From: GitHub Date: Thu, 5 Feb 2026 18:39:20 +0000 Subject: [PATCH 2/3] [auto-verifier] verify commit b20b54ce20d6bcaeb7ac2e52b91e868fe6e62f67 --- .verify-helper/timestamps.remote.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 4d3a648b..f410631d 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,8 +44,9 @@ "tests/library_checker_aizu_tests/data_structures/rmq_sparse_table.test.cpp": "2026-01-18 11:15:41 +0000", "tests/library_checker_aizu_tests/data_structures/rmq_sparse_table_inc.test.cpp": "2026-01-18 11:15:41 +0000", "tests/library_checker_aizu_tests/data_structures/simple_tree.test.cpp": "2026-01-28 21:53:13 -0700", -"tests/library_checker_aizu_tests/data_structures/simple_tree_inc.test.cpp": "2026-01-28 21:53:13 -0700", -"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_line.test.cpp": "2026-01-28 21:53:13 -0700", +"tests/library_checker_aizu_tests/data_structures/simple_tree_inc.test.cpp": "2026-02-05 11:36:54 -0700", +"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_line.test.cpp": "2026-02-05 11:36:54 -0700", +"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp": "2026-02-05 11:36:54 -0700", "tests/library_checker_aizu_tests/data_structures/simple_tree_line.test.cpp": "2026-01-28 21:53:13 -0700", "tests/library_checker_aizu_tests/flow/dinic_aizu.test.cpp": "2024-11-17 14:04:03 -0600", "tests/library_checker_aizu_tests/flow/hungarian.test.cpp": "2024-11-17 14:04:03 -0600", From 4e14e89ffebd1f1eb820331aa3366139df67ba13 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Thu, 5 Feb 2026 11:51:05 -0700 Subject: [PATCH 3/3] update docs --- library/data_structures_[l,r]/seg_tree.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/library/data_structures_[l,r]/seg_tree.hpp b/library/data_structures_[l,r]/seg_tree.hpp index 3cc7ee57..df0f4f57 100644 --- a/library/data_structures_[l,r]/seg_tree.hpp +++ b/library/data_structures_[l,r]/seg_tree.hpp @@ -2,11 +2,19 @@ //! https://codeforces.com/blog/entry/118682 //! @code //! { -//! tree st(n, 0LL, [&](ll l, ll r) { -//! return l + r; +//! tree st(a, [&](int vl, int vr) { +//! return vl & vr; //! }); //! } -//! tree st(n, INT_MAX, ranges::min); +//! { +//! tree st(n, 0LL, plus{}); +//! } +//! { +//! tree st(n, INT_MAX, ranges::min); +//! int idx = st.walk(l, r, [&](int value) { +//! return value <= x; +//! }); // smallest index in [l, r] s.t. f is true +//! } //! @endcode //! @time O(n + q log n) //! @space O(n)