Skip to content

Commit 47f30e9

Browse files
authored
Merge branch 'dev' into golf
2 parents b19e3d6 + 350f6d9 commit 47f30e9

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
"tests/library_checker_aizu_tests/data_structures/rmq_linear.test.cpp": "2026-01-18 11:15:41 +0000",
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",
46-
"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-02-07 17:15:13 -0700",
48-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_line.test.cpp": "2026-02-07 17:15:13 -0700",
49-
"tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp": "2026-02-07 17:42:33 -0700",
50-
"tests/library_checker_aizu_tests/data_structures/simple_tree_line.test.cpp": "2026-01-28 21:53:13 -0700",
5146
"tests/library_checker_aizu_tests/flow/dinic_aizu.test.cpp": "2024-11-17 14:04:03 -0600",
5247
"tests/library_checker_aizu_tests/flow/hungarian.test.cpp": "2024-11-17 14:04:03 -0600",
5348
"tests/library_checker_aizu_tests/flow/min_cost_max_flow.test.cpp": "2024-12-05 10:41:42 -0600",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#pragma once
22
//! https://github.com/kth-competitive-programming/kactl/blob/main/content/data-structures/SegmentTree.h
3+
//! https://codeforces.com/blog/entry/118682
34
//! @code
45
//! tree st1(n, INT_MAX, ranges::min);
56
//! tree st2(n, INT_MAX, [&](int x, int y) -> int {
67
//! return min(x, y);
78
//! });
9+
//! int idx = st2.walk(l, r, [&](int value) {
10+
//! return value > x;
11+
//! }); // min idx in [l, r) such that f is false
812
//! @endcode
913
//! @time O(n + q log n)
1014
//! @space O(n)
@@ -27,4 +31,5 @@ template<class T, class F> struct tree {
2731
}
2832
return op(x, y);
2933
}
34+
#include "seg_tree_uncommon/walk.hpp"
3035
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int walk(int l, int r, const auto& f) {
2+
while (l < r) {
3+
int u = l + n, x = __lg(min(u & -u, r - l));
4+
if (f(s[u >> x])) l += 1 << x;
5+
else r = l + (1 << x) - 1;
6+
}
7+
return l;
8+
}
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+
tree st(n, 0, plus<int>{});
10+
string s;
11+
cin >> s;
12+
for (int i = 0; i < n; i++) st.update(i, s[i] - '0');
13+
while (q--) {
14+
int type, k;
15+
cin >> type >> k;
16+
if (type == 0) {
17+
if (st.query(k, k + 1) == 0) st.update(k, 1);
18+
} else if (type == 1) {
19+
if (st.query(k, k + 1) == 1) st.update(k, 0);
20+
} else if (type == 2) {
21+
cout << st.query(k, k + 1) << '\n';
22+
} else if (type == 3) {
23+
// returns first element in [k,n) such that sum > 0
24+
int idx =
25+
st.walk(k, n, [&](int sum) { return sum == 0; });
26+
if (idx == n) idx = -1;
27+
cout << idx << '\n';
28+
} else {
29+
assert(type == 4);
30+
int total = st.query(0, k + 1);
31+
if (total == 0) {
32+
cout << -1 << '\n';
33+
} else {
34+
int pref_sum = 0;
35+
cout << st.walk(0, k + 1, [&](int sum) {
36+
if (pref_sum + sum < total)
37+
return pref_sum += sum, 1;
38+
return 0;
39+
}) << '\n';
40+
}
41+
}
42+
}
43+
return 0;
44+
}

0 commit comments

Comments
 (0)