From ab66d960dc4451cf1fa24d5b5d58f857af0d8663 Mon Sep 17 00:00:00 2001 From: arielherself Date: Wed, 18 Sep 2024 14:45:51 +0800 Subject: [PATCH] regular backup --- nvim/lua/snippets/bounded-mcmf.lua | 7 +++++-- nvim/lua/snippets/hld.lua | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/nvim/lua/snippets/bounded-mcmf.lua b/nvim/lua/snippets/bounded-mcmf.lua index 2064aad..62bf16c 100644 --- a/nvim/lua/snippets/bounded-mcmf.lua +++ b/nvim/lua/snippets/bounded-mcmf.lua @@ -2,11 +2,11 @@ return [=[ struct bounded_mcmf { int n, m, S, T; mcmf net; - ll sum; + ll sum, pre; vector fl; vector init; vector costs; - bounded_mcmf(int n, int m) : sum(0), n(n), m(m), S(0), T(n + 1), net(n + 1), fl(m), init(n + 1), costs(m) {} + bounded_mcmf(int n, int m) : sum(0), pre(0), n(n), m(m), S(0), T(n + 1), net(n + 1), fl(m), init(n + 1), costs(m) {} // handle negative loop case void add_edge(int from, int to, ll low, ll high, ll cost, int edge_id = -1) { if (cost < 0) { @@ -27,6 +27,7 @@ struct bounded_mcmf { void __add_edge(int from, int to, ll low, ll high, ll cost, int edge_id = -1) { net.add_edge(from, to, high - low, cost, edge_id, -1); init[to] += low, init[from] -= low; + pre += low * cost; } void prep(int s, int t) { for (int i = 1; i <= n; ++i) { @@ -57,6 +58,7 @@ struct bounded_mcmf { } } } + res_cost += pre; return {{res_flow, res_cost, fl}}; } } @@ -64,6 +66,7 @@ struct bounded_mcmf { optional>> run_mcf(int s, int t) { prep(s, t); auto [res_flow, res_cost] = net.run(S, T); + res_cost += pre; if (sum != res_flow) { return nullopt; } else { diff --git a/nvim/lua/snippets/hld.lua b/nvim/lua/snippets/hld.lua index c45bef1..332ab96 100644 --- a/nvim/lua/snippets/hld.lua +++ b/nvim/lua/snippets/hld.lua @@ -69,9 +69,10 @@ struct HLD { swap(u, v); } tr.range_apply(info[info[u].head].dfn, info[u].dfn, t); + u = info[info[u].head].father; } if (info[u].depth < info[v].depth) swap(u, v); - tr.range_apply(info[v].dfn, info[u].dfn); + tr.range_apply(info[v].dfn, info[u].dfn, t); } Info path_query(int u, int v) { @@ -81,6 +82,7 @@ struct HLD { swap(u, v); } res = res + tr.range_query(info[info[u].head].dfn, info[u].dfn); + u = info[info[u].head].father; } if (info[u].depth < info[v].depth) swap(u, v); res = res + tr.range_query(info[v].dfn, info[u].dfn);