Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/data_structures_[l,r)/bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct BIT {
for (; i < sz(s); i |= i + 1) s[i] += d;
}
ll query(int r) {
ll ret = 0;
for (; r > 0; r &= r - 1) ret += s[r - 1];
return ret;
ll res = 0;
for (; r > 0; r &= r - 1) res += s[r - 1];
return res;
}
ll query(int l, int r) { return query(r) - query(l); }
#include "bit_uncommon/walk_lambda.hpp"
Expand Down
8 changes: 4 additions & 4 deletions library/data_structures_[l,r)/bit_uncommon/kd_bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ template<int K> struct KD_BIT {
for (; i < sz(s); i |= i + 1) s[i].update(a...);
}
ll query(int l, int r, auto... a) {
ll ans = 0;
for (; l < r; r &= r - 1) ans += s[r - 1].query(a...);
for (; r < l; l &= l - 1) ans -= s[l - 1].query(a...);
return ans;
ll res = 0;
for (; l < r; r &= r - 1) res += s[r - 1].query(a...);
for (; r < l; l &= l - 1) res -= s[l - 1].query(a...);
return res;
}
};
template<> struct KD_BIT<0> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ struct kth_smallest {
int query(int k, int tl, int tr, int vl, int vr) {
if (tr - tl == 1) return tl;
int tm = tl + (tr - tl) / 2;
int left_count = pst.tree[pst.tree[vr].lch].sum -
pst.tree[pst.tree[vl].lch].sum;
if (left_count >= k)
int cnt_l = pst.tree[pst.tree[vr].lch].sum -
pst.tree[pst.tree[vl].lch].sum;
if (cnt_l >= k)
return query(k, tl, tm, pst.tree[vl].lch,
pst.tree[vr].lch);
return query(k - left_count, tm, tr, pst.tree[vl].rch,
return query(k - cnt_l, tm, tr, pst.tree[vl].rch,
pst.tree[vr].rch);
}
};
8 changes: 4 additions & 4 deletions library/data_structures_[l,r)/uncommon/deque_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ template<class T, class F> struct deq {
T front() { return (empty(l) ? r[0] : l.back())[0]; }
T back() { return (empty(r) ? l[0] : r.back())[0]; }
int siz() { return sz(l) + sz(r); }
void push_back(T elem) {
void push_back(T val) {
r.push_back(
{elem, empty(r) ? elem : op(r.back()[1], elem)});
{val, empty(r) ? val : op(r.back()[1], val)});
}
void pop_back() {
if (empty(r)) {
Expand All @@ -37,9 +37,9 @@ template<class T, class F> struct deq {
}
r.pop_back();
}
void push_front(T elem) {
void push_front(T val) {
l.push_back(
{elem, empty(l) ? elem : op(elem, l.back()[1])});
{val, empty(l) ? val : op(val, l.back()[1])});
}
void pop_front() {
if (empty(l)) {
Expand Down
20 changes: 10 additions & 10 deletions library/data_structures_[l,r)/uncommon/mode_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ const int b = 318; //!< sqrt(1e5)
//! https://noshi91.hatenablog.com/entry/2020/10/26/140105
struct mode_query {
int n;
vi a, cnt, index_into_index;
vector<vi> index;
vi a, cnt, pos_idx;
vector<vi> pos;
vector<vector<pii>>
mode_blocks; //!< {mode, cnt} of range of blocks
//! @param a compressed array: 0 <= a[i] < n
//! @time O(n * sqrt(n))
//! @space O(n)
mode_query(const vi& a):
n(sz(a)), a(a), cnt(n), index_into_index(n), index(n),
n(sz(a)), a(a), cnt(n), pos_idx(n), pos(n),
mode_blocks((n + b - 1) / b,
vector<pii>((n + b - 1) / b)) {
rep(i, 0, n) {
index_into_index[i] = sz(index[a[i]]);
index[a[i]].push_back(i);
pos_idx[i] = sz(pos[a[i]]);
pos[a[i]].push_back(i);
}
for (int start = 0; start < n; start += b) {
int mode = a[start];
Expand Down Expand Up @@ -51,17 +51,17 @@ struct mode_query {
for (int i = l / b * b + b - 1; i >= l; i--)
cnt[a[i]]++;
for (int i = l / b * b + b - 1; i >= l; i--) {
int idx = index_into_index[i];
if (idx + res.second < sz(index[a[i]]) &&
index[a[i]][idx + res.second] < r)
int idx = pos_idx[i];
if (idx + res.second < sz(pos[a[i]]) &&
pos[a[i]][idx + res.second] < r)
res = {a[i], cnt[a[i]] + res.second};
cnt[a[i]]--;
}
rep(i, (r - 1) / b * b, r) cnt[a[i]]++;
rep(i, (r - 1) / b * b, r) {
int idx = index_into_index[i];
int idx = pos_idx[i];
if (idx >= res.second &&
index[a[i]][idx - res.second] >= l)
pos[a[i]][idx - res.second] >= l)
res = {a[i], cnt[a[i]] + res.second};
cnt[a[i]]--;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! DS ds;
//! // int = argument type of DS::join
//! pq_updates<DS, int> pq(ds);
//! pq.push_update(val, priority);
//! pq.push_update(val, pri);
//! pq.pop_update();
//! @endcode
//! @time n interweaved calls to pop_update, push_update
Expand Down Expand Up @@ -38,10 +38,9 @@ template<class DS, class... ARGS> struct pq_updates {
extra.push_back(upd_st[idx_sk]);
idx = min(idx, idx_sk), lowest_pri = pri;
}
auto it =
remove_if(idx + all(upd_st), [&](auto& curr) {
return curr.second->first >= lowest_pri;
});
auto it = remove_if(idx + all(upd_st), [&](auto& cur) {
return cur.second->first >= lowest_pri;
});
reverse_copy(all(extra), it);
rep(i, idx, sz(upd_st)) ds.undo();
upd_st.pop_back();
Expand All @@ -53,13 +52,13 @@ template<class DS, class... ARGS> struct pq_updates {
}
}
//! @param args arguments to DS::join
//! @param priority must be distinct, can be negative
//! @param pri must be distinct, can be negative
//! @time O(log(n) + T(n))
//! @space an new update is allocated, inserted into
//! `upd_st`, `mp` member variables
void push_update(ARGS... args, int priority) {
void push_update(ARGS... args, int pri) {
ds.join(args...);
auto [it, ins] = mp.emplace(priority, sz(upd_st));
auto [it, ins] = mp.emplace(pri, sz(upd_st));
assert(ins);
upd_st.emplace_back(make_tuple(args...), it);
}
Expand Down
6 changes: 3 additions & 3 deletions library/data_structures_[l,r]/bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct BIT {
for (; i < sz(s); i |= i + 1) s[i] += d;
}
ll query(int i) {
ll ret = 0;
for (; i >= 0; (i &= i + 1)--) ret += s[i];
return ret;
ll res = 0;
for (; i >= 0; (i &= i + 1)--) res += s[i];
return res;
}
ll query(int l, int r) {
return query(r) - query(l - 1);
Expand Down
4 changes: 2 additions & 2 deletions library/dsu/line_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//! line_tree lt(n);
//! for (auto [w, u, v] : w_eds) lt.join(u, v);
//! for (int v = lt.f(0); v != -1;) {
//! auto [next, e_id] = lt.edge[v];
//! auto [nxt, e_id] = lt.edge[v];
//! int w = w_eds[e_id][0];
//! //
//! v = next;
//! v = nxt;
//! }
//! @endcode
//! lt.f(v) = head of linked list
Expand Down
8 changes: 4 additions & 4 deletions library/flow/hungarian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ pair<ll, vi> hungarian(const vector<vector<ll>>& cost) {
p[0] = i;
int j0 = 0;
vector minv(m, LLONG_MAX);
vi used(m);
vi vis(m);
do {
used[j0] = 1;
vis[j0] = 1;
int i0 = p[j0], j1 = 0;
ll delta = LLONG_MAX;
rep(j, 1, m) if (!used[j]) {
rep(j, 1, m) if (!vis[j]) {
ll cur = cost[i0][j] - u[i0] - v[j];
if (cur < minv[j]) minv[j] = cur, way[j] = j0;
if (minv[j] < delta) delta = minv[j], j1 = j;
}
rep(j, 0, m) {
if (used[j]) u[p[j]] += delta, v[j] -= delta;
if (vis[j]) u[p[j]] += delta, v[j] -= delta;
else minv[j] -= delta;
}
j0 = j1;
Expand Down
12 changes: 6 additions & 6 deletions library/graphs/bcc_callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
//! {
//! vector<vi> g(n);
//! DSU dsu(n);
//! vector<bool> seen(n);
//! vector<bool> vis(n);
//! bcc(g, [&](const vi& nodes) {
//! int count_edges = 0;
//! int cnt_edges = 0;
//! rep (i, 0, sz(nodes) - 1) {
//! seen[nodes[i]] = 1;
//! for (int u : g[nodes[i]]) if (!seen[u]) {
//! vis[nodes[i]] = 1;
//! for (int u : g[nodes[i]]) if (!vis[u]) {
//! // edge nodes[i] <=> u is in current BCC
//! count_edges++;
//! cnt_edges++;
//! }
//! }
//! if (count_edges == 1) {
//! if (cnt_edges == 1) {
//! // nodes[0] <=> nodes[1] is a bridge
//! return;
//! }
Expand Down
8 changes: 4 additions & 4 deletions library/graphs/uncommon/enumerate_triangles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ void enumerate_triangles(const vector<pii>& edges, int n,
if (tie(deg[u], u) > tie(deg[v], v)) swap(u, v);
g[u].push_back(v);
}
vector<bool> seen(n);
vector<bool> vis(n);
for (auto [u, v] : edges) {
for (int w : g[u]) seen[w] = 1;
for (int w : g[u]) vis[w] = 1;
for (int w : g[v])
if (seen[w]) f(u, v, w);
for (int w : g[u]) seen[w] = 0;
if (vis[w]) f(u, v, w);
for (int w : g[u]) vis[w] = 0;
}
}
16 changes: 8 additions & 8 deletions library/math/count_paths/count_paths_rectangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
//! @time O((n + m)log(n + m))
//! @space O(n + m)
array<vl, 2> get_right_and_top(vl left, vl bottom) {
array<vl, 2> ret;
for (vl& res : ret) {
array<vl, 2> res;
for (vl& cur : res) {
{
vl tr(sz(left));
rep(i, 0, sz(tr)) tr[i] = C(i + sz(bottom) - 1, i);
res = conv(left, tr);
res.resize(sz(left));
cur = conv(left, tr);
cur.resize(sz(left));
}
{
vl tr(sz(left) + sz(bottom));
Expand All @@ -23,13 +23,13 @@ array<vl, 2> get_right_and_top(vl left, vl bottom) {
vl dp(sz(bottom));
rep(i, 0, sz(dp)) dp[i] =
bottom[i] * t[sz(dp) - 1 - i].inv_fact % mod;
vl tmp_res = conv(dp, tr);
rep(i, 0, sz(res))
res[i] = (res[i] + tmp_res[i + sz(bottom) - 1] *
vl tmp = conv(dp, tr);
rep(i, 0, sz(cur))
cur[i] = (cur[i] + tmp[i + sz(bottom) - 1] *
t[i].inv_fact) %
mod;
}
swap(left, bottom);
}
return ret;
return res;
}
8 changes: 4 additions & 4 deletions library/math/num_distinct_subsequences.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ int num_subsequences(const vi& a, int mod) {
vector dp(sz(a) + 1, 1);
map<int, int> last;
rep(i, 0, sz(a)) {
int& curr = dp[i + 1] = 2 * dp[i];
if (curr >= mod) curr -= mod;
int& cur = dp[i + 1] = 2 * dp[i];
if (cur >= mod) cur -= mod;
auto it = last.find(a[i]);
if (it != end(last)) {
curr -= dp[it->second];
if (curr < 0) curr += mod;
cur -= dp[it->second];
if (cur < 0) cur += mod;
it->second = i;
} else last[a[i]] = i;
}
Expand Down
12 changes: 6 additions & 6 deletions library/strings/binary_trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ const ll mx_bit = 1LL << 60;
struct binary_trie {
struct node {
int siz = 0;
array<int, 2> next = {-1, -1};
array<int, 2> nxt = {-1, -1};
};
deque<node> t;
binary_trie(): t(1) {}
void update(ll num, int delta) {
int v = 0;
for (ll bit = mx_bit; bit; bit /= 2) {
bool b = num & bit;
if (t[v].next[b] == -1) {
t[v].next[b] = sz(t);
if (t[v].nxt[b] == -1) {
t[v].nxt[b] = sz(t);
t.emplace_back();
}
v = t[v].next[b];
v = t[v].nxt[b];
t[v].siz += delta;
}
}
Expand All @@ -31,9 +31,9 @@ struct binary_trie {
ll res = 0;
for (ll bit = mx_bit; bit; bit /= 2) {
bool b = num & bit;
int u = t[v].next[b];
int u = t[v].nxt[b];
if (u != -1 && t[u].siz > 0) v = u, res |= num & bit;
else v = t[v].next[!b], res |= (~num) & bit;
else v = t[v].nxt[!b], res |= (~num) & bit;
}
return res;
}
Expand Down
14 changes: 7 additions & 7 deletions library/strings/trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
const int mn = 'A';
struct trie {
struct node {
array<int, 26> next;
array<int, 26> nxt;
bool end_of_word = 0;
node() { ranges::fill(next, -1); }
node() { ranges::fill(nxt, -1); }
};
deque<node> t;
trie(): t(1) {}
void insert(const string& s) {
int v = 0;
for (char ch : s) {
int u = ch - mn;
if (t[v].next[u] == -1) {
t[v].next[u] = sz(t);
if (t[v].nxt[u] == -1) {
t[v].nxt[u] = sz(t);
t.emplace_back();
}
v = t[v].next[u];
v = t[v].nxt[u];
}
t[v].end_of_word = 1;
}
int find(const string& s) {
int v = 0;
for (char ch : s) {
int u = ch - mn;
if (t[v].next[u] == -1) return 0;
v = t[v].next[u];
if (t[v].nxt[u] == -1) return 0;
v = t[v].nxt[u];
}
return t[v].end_of_word;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ int main() {
int type, x;
cin >> type >> x;
if (type == 0) {
if (
bt.t[bt.t[0].next[0]].siz == 0 || bt.walk(x) != x)
if (bt.t[bt.t[0].nxt[0]].siz == 0 || bt.walk(x) != x)
bt.update(x, 1);
} else if (type == 1) {
if (bt.t[bt.t[0].next[0]].siz > 0 && bt.walk(x) == x)
if (bt.t[bt.t[0].nxt[0]].siz > 0 && bt.walk(x) == x)
bt.update(x, -1);
} else {
assert(type == 2);
Expand Down
Loading