Skip to content

Commit 24829a8

Browse files
committed
update
1 parent 1bcac13 commit 24829a8

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

library/data_structures_[l,r)/seg_tree_uncommon/min_left.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ int min_left(int l, int r, const auto& f) {
55
r -= 1 << v, x = y;
66
else l = r - (1 << v) + 1;
77
}
8-
return l;
8+
return r;
99
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
int min_left(int l, int r, const auto& f) {
22
if (T x = s[r + n]; f(x))
3-
while (l < r) {
4-
int u = r + n, v = __lg(min(u & -u, r - l));
3+
for (r--; l <= r;) {
4+
cout << "l,r:" << l << ' ' << r << endl; // 0 1
5+
int u = r + 1 + n, v = __lg(min(u & -u, r - l + 1));
56
if (T y = op(s[(u - 1) >> v], x); f(y))
67
r -= 1 << v, x = y;
78
else l = r - (1 << v) + 1;
89
}
9-
return l;
10+
return r;
1011
}

tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main() {
1010
cin >> s;
1111
vector<int> init(n);
1212
for (int i = 0; i < n; i++) init[i] = s[i] - '0';
13-
tree st(init, plus<int>{});
13+
tree st(init, ranges::max);
1414
while (q--) {
1515
int type, k;
1616
cin >> type >> k;
@@ -21,21 +21,16 @@ int main() {
2121
} else if (type == 2) {
2222
cout << st.query(k, k) << '\n';
2323
} else if (type == 3) {
24-
// returns first element in [k,n-1] such that sum > 0
24+
// returns first element in [k,n-1] such that mx > 0
2525
int idx = st.max_right(k, n - 1,
26-
[&](int sum) { return sum == 0; });
26+
[&](int mx) { return mx == 0; });
2727
if (idx == n) idx = -1;
2828
cout << idx << '\n';
2929
} else {
3030
assert(type == 4);
31-
int total = st.query(0, k);
32-
if (total == 0) {
33-
cout << -1 << '\n';
34-
} else {
35-
cout << st.max_right(0, k, [&](int sum) {
36-
return sum < total;
37-
}) << '\n';
38-
}
31+
cout << st.min_left(0, k, [&](int mx) {
32+
return mx == 0;
33+
}) << '\n';
3934
}
4035
}
4136
return 0;

tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main() {
66
cin.tie(0)->sync_with_stdio(0);
77
int n, q;
88
cin >> n >> q;
9-
tree st(n, 0, plus<int>{});
9+
tree st(n, INT_MIN, ranges::max);
1010
string s;
1111
cin >> s;
1212
for (int i = 0; i < n; i++) st.update(i, s[i] - '0');
@@ -20,21 +20,16 @@ int main() {
2020
} else if (type == 2) {
2121
cout << st.query(k, k + 1) << '\n';
2222
} else if (type == 3) {
23-
// returns first element in [k,n) such that sum > 0
23+
// returns first element in [k,n) such that mx > 0
2424
int idx = st.max_right(k, n,
25-
[&](int sum) { return sum == 0; });
25+
[&](int mx) { return mx == 0; });
2626
if (idx == n) idx = -1;
2727
cout << idx << '\n';
2828
} else {
2929
assert(type == 4);
30-
int total = st.query(0, k + 1);
31-
if (total == 0) {
32-
cout << -1 << '\n';
33-
} else {
34-
cout << st.max_right(0, k + 1, [&](int sum) {
35-
return sum < total;
36-
}) << '\n';
37-
}
30+
cout << st.min_left(0, k + 1,
31+
[&](int mx) { return mx == 0; }) -
32+
1 << '\n';
3833
}
3934
}
4035
return 0;

0 commit comments

Comments
 (0)