From 42760e8332cf91076c48d0ae8efd875767e1f6f0 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Sat, 23 May 2026 17:07:34 -0500 Subject: [PATCH 1/3] golf --- library/data_structures_[l,r]/linear_rmq.hpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/library/data_structures_[l,r]/linear_rmq.hpp b/library/data_structures_[l,r]/linear_rmq.hpp index c48844f4..52d5d965 100644 --- a/library/data_structures_[l,r]/linear_rmq.hpp +++ b/library/data_structures_[l,r]/linear_rmq.hpp @@ -19,17 +19,16 @@ template struct linear_rmq { vi in, asc, head; linear_rmq(const vector& a, F cmp): n(sz(a)), a(a), cmp(cmp), in(n), asc(n), head(n + 1) { - vi st{-1}; + vi st(n + 1, -1); + int t = 0; rep(i, 0, n + 1) { int prev = 0; - while (sz(st) > 1 && - (i == n || !cmp(a[st.back()], a[i]))) { - head[prev] = st.back(); - auto j = end(st)[-2] + 1u, k = bit_floor(i ^ j); - in[st.back()] = prev = i & -k, asc[j] |= k; - st.pop_back(); + while (t && (i == n || !cmp(a[st[t]], a[i]))) { + head[prev] = st[t]; + auto j = st[t - 1] + 1u, k = bit_floor(i ^ j); + in[st[t--]] = prev = i & -k, asc[j] |= k; } - st.push_back(head[prev] = i); + st[++t] = head[prev] = i; } rep(i, 1, n) asc[i] = (asc[i] | asc[i - 1]) & -(in[i] & -in[i]); From 83c9fa0f46166e9193d3f90ae61f855056ec2e6d Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Sat, 23 May 2026 17:11:17 -0500 Subject: [PATCH 2/3] fix --- tests/.config/.cppcheck_suppression_list | 1 - .../data_structures/rmq_linear.test.cpp | 15 --------------- 2 files changed, 16 deletions(-) diff --git a/tests/.config/.cppcheck_suppression_list b/tests/.config/.cppcheck_suppression_list index 3dc20270..4cdfb366 100644 --- a/tests/.config/.cppcheck_suppression_list +++ b/tests/.config/.cppcheck_suppression_list @@ -53,7 +53,6 @@ constVariableReference:library_checker_aizu_tests/handmade_tests/dsu_size.test.c constVariableReference:../library/trees/centroid_decomp_uncommon/count_paths_per_length.hpp:34 constVariablePointer:../kactl/content/numerical/FastFourierTransform.h:39 cstyleCast:../kactl/content/numerical/FastFourierTransform.h:39 -derefInvalidIterator:../library/data_structures_[l,r]/linear_rmq.hpp:28 derefInvalidIterator:library_checker_aizu_tests/handmade_tests/n_choose_k.test.cpp:13 unreadVariable:library_checker_aizu_tests/handmade_tests/permutation_tree_small.test.cpp:12 uninitvar:library_checker_aizu_tests/handmade_tests/seg_tree_find_small.test.cpp:41 diff --git a/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp b/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp index ade17e8f..8f96e725 100644 --- a/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp +++ b/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp @@ -8,11 +8,6 @@ int main() { vector a(n); for (int i = 0; i < n; i++) cin >> a[i]; linear_rmq rmq_less(a, less()); - linear_rmq rmq_less_equal(a, less_equal()); - vector neg_a(n); - for (int i = 0; i < n; i++) neg_a[i] = -a[i]; - linear_rmq rmq_greater(neg_a, greater()); - linear_rmq rmq_greater_equal(neg_a, greater_equal()); while (q--) { int l, r; cin >> l >> r; @@ -22,16 +17,6 @@ int main() { a[idx_right_min]); assert(l <= idx_right_min && idx_right_min < r); assert(rmq_less.query(l, r - 1) == a[idx_right_min]); - assert(idx_right_min == rmq_greater.idx(l, r - 1)); - int idx_left_min = rmq_less_equal.idx(l, r - 1); - assert(l == idx_left_min || - rmq_less_equal.query(l, idx_left_min - 1) > - a[idx_left_min]); - assert(l <= idx_left_min && idx_left_min < r); - assert( - idx_left_min == rmq_greater_equal.idx(l, r - 1)); - assert(a[idx_right_min] == a[idx_left_min]); - assert(idx_left_min <= idx_right_min); cout << a[idx_right_min] << '\n'; } } From 80822850bca5b84cf018fd3aa813dfb05f847bad Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Sat, 23 May 2026 17:13:33 -0500 Subject: [PATCH 3/3] revert --- .../data_structures/rmq_linear.test.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp b/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp index 8f96e725..ade17e8f 100644 --- a/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp +++ b/tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp @@ -8,6 +8,11 @@ int main() { vector a(n); for (int i = 0; i < n; i++) cin >> a[i]; linear_rmq rmq_less(a, less()); + linear_rmq rmq_less_equal(a, less_equal()); + vector neg_a(n); + for (int i = 0; i < n; i++) neg_a[i] = -a[i]; + linear_rmq rmq_greater(neg_a, greater()); + linear_rmq rmq_greater_equal(neg_a, greater_equal()); while (q--) { int l, r; cin >> l >> r; @@ -17,6 +22,16 @@ int main() { a[idx_right_min]); assert(l <= idx_right_min && idx_right_min < r); assert(rmq_less.query(l, r - 1) == a[idx_right_min]); + assert(idx_right_min == rmq_greater.idx(l, r - 1)); + int idx_left_min = rmq_less_equal.idx(l, r - 1); + assert(l == idx_left_min || + rmq_less_equal.query(l, idx_left_min - 1) > + a[idx_left_min]); + assert(l <= idx_left_min && idx_left_min < r); + assert( + idx_left_min == rmq_greater_equal.idx(l, r - 1)); + assert(a[idx_right_min] == a[idx_left_min]); + assert(idx_left_min <= idx_right_min); cout << a[idx_right_min] << '\n'; } }