Skip to content

Commit 84fd1d8

Browse files
author
Cameron Custer
committed
stl audit: midpoint in recursive seg trees; emplace+exchange in num_distinct_subsequences
1 parent aeee182 commit 84fd1d8

4 files changed

Lines changed: 9 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ template<int N> struct implicit_seg_tree {
4242
int v) {
4343
if (r <= tl || tr <= l) return;
4444
if (l <= tl && tr <= r) return apply(add, v);
45-
int tm = tl + (tr - tl) / 2;
45+
int tm = midpoint(tl, tr);
4646
push(tl, tm, tr, v);
4747
update(l, r, add, tl, tm, tree[v].lch);
4848
update(l, r, add, tm, tr, tree[v].rch);
@@ -55,7 +55,7 @@ template<int N> struct implicit_seg_tree {
5555
dt query(int l, int r, int tl, int tr, int v) {
5656
if (r <= tl || tr <= l) return unit;
5757
if (l <= tl && tr <= r) return tree[v].num;
58-
int tm = tl + (tr - tl) / 2;
58+
int tm = midpoint(tl, tr);
5959
push(tl, tm, tr, v);
6060
return op(query(l, r, tl, tm, tree[v].lch),
6161
query(l, r, tm, tr, tree[v].rch));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct kth_smallest {
2525
}
2626
int query(int k, int tl, int tr, int vl, int vr) {
2727
if (tr - tl == 1) return tl;
28-
int tm = tl + (tr - tl) / 2;
28+
int tm = midpoint(tl, tr);
2929
int cnt_l = pst.tree[pst.tree[vr].lch].sum -
3030
pst.tree[pst.tree[vl].lch].sum;
3131
if (cnt_l >= k)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct PST {
3030
tree.emplace_back(tree[v].sum + change, 0, 0);
3131
return sz(tree) - 1;
3232
}
33-
int tm = tl + (tr - tl) / 2;
33+
int tm = midpoint(tl, tr);
3434
int lch = tree[v].lch;
3535
int rch = tree[v].rch;
3636
if (idx < tm) lch = update(idx, change, tl, tm, lch);
@@ -45,7 +45,7 @@ struct PST {
4545
ll query(int l, int r, int tl, int tr, int v) {
4646
if (v == 0 || r <= tl || tr <= l) return 0;
4747
if (l <= tl && tr <= r) return tree[v].sum;
48-
int tm = tl + (tr - tl) / 2;
48+
int tm = midpoint(tl, tr);
4949
return query(l, r, tl, tm, tree[v].lch) +
5050
query(l, r, tm, tr, tree[v].rch);
5151
}

library/math/num_distinct_subsequences.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ int num_subsequences(const vi& a, int mod) {
99
rep(i, 0, sz(a)) {
1010
int& cur = dp[i + 1] = 2 * dp[i];
1111
if (cur >= mod) cur -= mod;
12-
auto it = last.find(a[i]);
13-
if (it != end(last)) {
14-
cur -= dp[it->second];
12+
auto [it, ins] = last.emplace(a[i], i);
13+
if (!ins) {
14+
cur -= dp[exchange(it->second, i)];
1515
if (cur < 0) cur += mod;
16-
it->second = i;
17-
} else last[a[i]] = i;
16+
}
1817
}
1918
return dp.back();
2019
}

0 commit comments

Comments
 (0)