Skip to content

Commit eef796d

Browse files
authored
add it to inc exc now (#180)
1 parent f808141 commit eef796d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
//! tree st2(n, INT_MAX, [&](int x, int y) -> int {
66
//! return min(x, y);
77
//! });
8+
//! int idx = st2.walk(l, r, [&](int value) {
9+
//! return value > x;
10+
//! }); // min idx in [l, r) such that f is false
811
//! @endcode
912
//! @time O(n + q log n)
1013
//! @space O(n)
@@ -27,4 +30,5 @@ template<class T, class F> struct tree {
2730
}
2831
return op(x, y);
2932
}
33+
#include "seg_tree_uncommon/walk.hpp"
3034
};
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)