Skip to content

Commit 070a61a

Browse files
committed
data_structures: use >> 1 instead of / 2 for non-negative halving
1 parent 5feab23 commit 070a61a

7 files changed

Lines changed: 10 additions & 10 deletions

File tree

library/data_structures_[l,r)/bit_uncommon/walk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
int walk(ll sum) {
22
if (sum <= 0) return -1;
33
int r = 0;
4-
for (int i = bit_floor(size(s)); i; i /= 2)
4+
for (int i = bit_floor(size(s)); i; i >>= 1)
55
if (r + i <= sz(s) && s[r + i - 1] < sum)
66
sum -= s[(r += i) - 1];
77
return r;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ template<class T, class F> struct tree {
2727
tree(int n, T unit, F op):
2828
n(n), unit(unit), op(op), s(2 * n, unit) {}
2929
void update(int i, T val) {
30-
for (s[i += n] = val; i /= 2;)
30+
for (s[i += n] = val; i >>= 1;)
3131
s[i] = op(s[2 * i], s[2 * i + 1]);
3232
}
3333
T query(int l, int r) {
3434
T x = unit, y = unit;
35-
for (l += n, r += n; l < r; l /= 2, r /= 2) {
35+
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
3636
if (l % 2) x = op(x, s[l++]);
3737
if (r % 2) y = op(s[--r], y);
3838
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
//! https://codeforces.com/blog/entry/112755
33
int split(int tl, int tr) {
44
int pw2 = bit_floor(tr - tl + 0u);
5-
return min(tl + pw2, tr - pw2 / 2);
5+
return min(tl + pw2, tr - (pw2 >> 1));
66
}

library/data_structures_[l,r)/uncommon/deque_op.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ template<class T, class F> struct deq {
1010
F op;
1111
vector<dt> l, r;
1212
deq(const vector<T>& a, F op): op(op) {
13-
rebuild(a, sz(a) / 2);
13+
rebuild(a, sz(a) >> 1);
1414
}
1515
T query() {
1616
if (empty(l)) return r.back()[1];
@@ -33,7 +33,7 @@ template<class T, class F> struct deq {
3333
vector<T> a(sz(l));
3434
ranges::transform(l, rbegin(a),
3535
[](dt& x) { return x[0]; });
36-
rebuild(a, sz(a) / 2);
36+
rebuild(a, sz(a) >> 1);
3737
}
3838
r.pop_back();
3939
}
@@ -46,7 +46,7 @@ template<class T, class F> struct deq {
4646
vector<T> a(sz(r));
4747
ranges::transform(r, begin(a),
4848
[](dt& x) { return x[0]; });
49-
rebuild(a, (sz(a) + 1) / 2);
49+
rebuild(a, (sz(a) + 1) >> 1);
5050
}
5151
l.pop_back();
5252
}

library/data_structures_[l,r)/uncommon/hilbert_mos.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
ll hilbert(int x, int y) {
1212
ll d = 0, mx = 1;
1313
while (mx <= max(x, y)) mx *= 4;
14-
for (int s = mx / 2; s; s /= 2) {
14+
for (int s = mx >> 1; s; s >>= 1) {
1515
bool rx = x & s, ry = y & s;
1616
d = d * 4 | (ry * 3 ^ rx);
1717
if (!rx) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ template<class T, class F> struct tree {
3030
n(n), op(op), s(2 * n, unit) {}
3131
#include "seg_tree_uncommon/init.hpp"
3232
void update(int i, T val) {
33-
for (s[i += n] = val; i /= 2;)
33+
for (s[i += n] = val; i >>= 1;)
3434
s[i] = op(s[2 * i], s[2 * i + 1]);
3535
}
3636
T query(int l, int r) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
//! @endcode
77
int split(int tl, int tr) {
88
int pw2 = bit_floor(tr - tl + 1u);
9-
return min(tl + pw2 - 1, tr - pw2 / 2);
9+
return min(tl + pw2 - 1, tr - (pw2 >> 1));
1010
}

0 commit comments

Comments
 (0)