Skip to content

Commit f7a9f8f

Browse files
committed
add walk for [l,r] now
1 parent 0cdd3be commit f7a9f8f

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ struct BIT {
1717
ll query(int l, int r) {
1818
return query(r) - query(l - 1);
1919
}
20+
#include "bit_uncommon/walk_lambda.hpp"
2021
#include "../data_structures_[l,r)/bit_uncommon/walk.hpp"
2122
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
void walk(const auto& f) {
2+
ll sum = 0;
3+
for (int i = bit_floor(size(s)), r = 0; i; i /= 2)
4+
if (r + i <= sz(s) && f(r + i - 1, sum + s[r + i - 1]))
5+
sum += s[(r += i) - 1];
6+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#define PROBLEM \
2+
"https://judge.yosupo.jp/problem/predecessor_problem"
3+
#include "../template.hpp"
4+
#include "../../../library/data_structures_[l,r]/bit.hpp"
5+
int main() {
6+
cin.tie(0)->sync_with_stdio(0);
7+
int n, q;
8+
string s;
9+
cin >> n >> q >> s;
10+
vector<ll> init(n);
11+
for (int i = 0; i < n; i++) init[i] = s[i] - '0';
12+
BIT bit(init);
13+
while (q--) {
14+
int type, k;
15+
cin >> type >> k;
16+
if (type == 0) {
17+
if (bit.query(k, k) == 0) bit.update(k, 1);
18+
} else if (type == 1) {
19+
if (bit.query(k, k) == 1) bit.update(k, -1);
20+
} else if (type == 2) {
21+
cout << bit.query(k, k) << '\n';
22+
} else if (type == 3) {
23+
if (bit.query(k, n - 1) == 0) cout << -1 << '\n';
24+
else {
25+
ll order = bit.query(k - 1);
26+
int res = -1;
27+
bit.walk([&](int r, ll sum) {
28+
if (sum <= order) return 1;
29+
res = r;
30+
return 0;
31+
});
32+
cout << res << '\n';
33+
}
34+
} else {
35+
if (bit.query(k) == 0) cout << -1 << '\n';
36+
else {
37+
ll order = bit.query(k);
38+
int res = -1;
39+
bit.walk([&](int r, ll sum) {
40+
if (sum >= order) {
41+
res = r;
42+
return 0;
43+
}
44+
return 1;
45+
});
46+
cout << res << '\n';
47+
}
48+
}
49+
}
50+
return 0;
51+
}

0 commit comments

Comments
 (0)