Skip to content

Commit

Permalink
clear before reserve
Browse files Browse the repository at this point in the history
  • Loading branch information
old-yan committed Jul 7, 2024
1 parent eaf0421 commit cd0b24d
Show file tree
Hide file tree
Showing 24 changed files with 158 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DS/DynamicBitset.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

1. [2166. 设计位集](https://leetcode.cn/problems/design-bitset/)
2. [3161. 物块放置查询](https://leetcode.cn/problems/block-placement-queries/)
3. [3182. 执行操作可获得的最大总奖励 II](https://leetcode.cn/problems/maximum-total-reward-using-operations-ii/)
3. [3181. 执行操作可获得的最大总奖励 II](https://leetcode.cn/problems/maximum-total-reward-using-operations-ii/)
4. [#2037. 「SHOI2015」脑洞治疗仪](https://loj.ac/p/2037)
5. [P1503 鬼子进村](https://www.luogu.com.cn/problem/P1503)
6. [P2572 [SCOI2010] 序列操作](https://www.luogu.com.cn/problem/P2572)
Expand Down
12 changes: 7 additions & 5 deletions DS/LinkBucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
10. [P4556 [Vani有约会] 雨天的尾巴 /【模板】线段树合并](https://www.luogu.com.cn/problem/P4556)
11. [P5287 [HNOI2019] JOJO](https://www.luogu.com.cn/problem/P5287)
12. [U208135 可持久化并查集 加强版](https://www.luogu.com.cn/problem/U208135)
13. [#515. 【UR #19】前进四](https://uoj.ac/problem/515)
14. [Persistent Queue](https://judge.yosupo.jp/problem/persistent_queue)(https://github.com/yosupo06/library-checker-problems/issues/379)
15. [Persistent Unionfind](https://judge.yosupo.jp/problem/persistent_unionfind)(https://github.com/yosupo06/library-checker-problems/issues/405)
16. [Cycle Detection (Directed)](https://judge.yosupo.jp/problem/cycle_detection)(https://github.com/yosupo06/library-checker-problems/issues/534)
17. [Cycle Detection (Undirected)](https://judge.yosupo.jp/problem/cycle_detection_undirected)(https://github.com/yosupo06/library-checker-problems/issues/869)
13. [parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852)
14. [#515. 【UR #19】前进四](https://uoj.ac/problem/515)
15. [Persistent Queue](https://judge.yosupo.jp/problem/persistent_queue)(https://github.com/yosupo06/library-checker-problems/issues/379)
16. [Persistent Unionfind](https://judge.yosupo.jp/problem/persistent_unionfind)(https://github.com/yosupo06/library-checker-problems/issues/405)
17. [Cycle Detection (Directed)](https://judge.yosupo.jp/problem/cycle_detection)(https://github.com/yosupo06/library-checker-problems/issues/534)
18. [Cycle Detection (Undirected)](https://judge.yosupo.jp/problem/cycle_detection_undirected)(https://github.com/yosupo06/library-checker-problems/issues/869)




Expand Down
2 changes: 1 addition & 1 deletion DS/OfflineMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace OY {
std::vector<pair> ps(length);
for (size_type i = 0; i != length; i++) ps[i] = {Tp(mapping(i)), i};
std::sort(ps.begin(), ps.end());
m_sorted.reserve(ps.size());
m_sorted.clear(), m_sorted.reserve(ps.size());
m_arr.resize(length);
for (size_type i = 0; i != length; i++) {
if (!i || ps[i - 1].m_val < ps[i].m_val) m_sorted.push_back(ps[i].m_val);
Expand Down
2 changes: 1 addition & 1 deletion DS/RangeMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace OY {
std::vector<Pair<Tp>> ps(m_size);
for (size_type i = 0; i != m_size; i++) ps[i] = {Tp(mapping(i)), i};
std::sort(ps.begin(), ps.end(), [](const Pair<Tp> &x, const Pair<Tp> &y) { return x.m_val < y.m_val; });
m_sorted.reserve(m_size);
m_sorted.clear(), m_sorted.reserve(m_size);
m_arr.resize(m_size);
for (size_type i = 0; i != m_size; i++) {
if (!i || ps[i].m_val != ps[i - 1].m_val) m_sorted.push_back(ps[i].m_val);
Expand Down
1 change: 1 addition & 0 deletions DS/SegCounter.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
3. [P6136 【模板】普通平衡树(数据加强版)](https://www.luogu.com.cn/problem/P6136)
4. [P8496 [NOI2022] 众数](https://www.luogu.com.cn/problem/P8496)
5. [P9233 [蓝桥杯 2023 省 A] 颜色平衡树](https://www.luogu.com.cn/problem/P9233)
6. [parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852)


### 二、模板功能
Expand Down
2 changes: 1 addition & 1 deletion DS/StaticBitset.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
​ 练习题目:

1. [3161. 物块放置查询](https://leetcode.cn/problems/block-placement-queries/)
2. [3182. 执行操作可获得的最大总奖励 II](https://leetcode.cn/problems/maximum-total-reward-using-operations-ii/)
2. [3181. 执行操作可获得的最大总奖励 II](https://leetcode.cn/problems/maximum-total-reward-using-operations-ii/)


### 二、模板功能
Expand Down
4 changes: 2 additions & 2 deletions DS/WaveLet.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ namespace OY {
if (i && comp(items[i - 1].m_val, items[i].m_val)) cnt++;
id[items[i].m_index] = cnt;
}
m_discretizer.reserve(cnt + 1);
m_discretizer.clear(), m_discretizer.reserve(cnt + 1);
std::unique_copy(items.begin(), items.end(), std::back_insert_iterator<std::vector<Tp>>(m_discretizer));
if constexpr (std::is_same<TableMapping, Ignore>::value)
m_table.reset(id.begin(), id.end(), std::bit_width(cnt + 1), [&](size_type val) { return m_discretizer[val]; });
Expand All @@ -349,7 +349,7 @@ namespace OY {
if (i && comp(items[i - 1].m_val, items[i].m_val)) cnt++;
id[items[i].m_index] = cnt;
}
m_discretizer.reserve(cnt + 1);
m_discretizer.clear(), m_discretizer.reserve(cnt + 1);
std::unique_copy(items.begin(), items.end(), std::back_insert_iterator<std::vector<Tp>>(m_discretizer));
m_table.reset_mapping_with_index(id.begin(), id.end(), std::bit_width(cnt + 1), [&](size_type val, size_type i) { return table_mapping(m_discretizer[val], i); });
}
Expand Down
2 changes: 1 addition & 1 deletion GRAPH/Dinic.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace OY {
Graph(size_type vertex_cnt = 0, size_type edge_cnt = 0) { resize(vertex_cnt, edge_cnt); }
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!(m_vertex_cnt = vertex_cnt)) return;
m_prepared = false, m_raw_edges.reserve(edge_cnt);
m_prepared = false, m_raw_edges.clear(), m_raw_edges.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType cap) { m_raw_edges.push_back({from, to, cap}); }
template <typename SumType = FlowType>
Expand Down
8 changes: 4 additions & 4 deletions GRAPH/Dinic_mcmf.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace OY {
Graph(size_type vertex_cnt = 0, size_type edge_cnt = 0) { resize(vertex_cnt, edge_cnt); }
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!(m_vertex_cnt = vertex_cnt)) return;
m_prepared = false, m_distance.resize(m_vertex_cnt), m_iter.resize(m_vertex_cnt), m_raw_edges.reserve(edge_cnt);
m_prepared = false, m_distance.resize(m_vertex_cnt), m_iter.resize(m_vertex_cnt), m_raw_edges.clear(), m_raw_edges.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType cap, CostType cost) { m_raw_edges.push_back({from, to, cap, cost}); }
template <typename FlowSumType = FlowType, typename CostSumType = CostType>
Expand Down Expand Up @@ -198,7 +198,7 @@ namespace OY {
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!vertex_cnt) return;
m_graph.resize(vertex_cnt + 2, edge_cnt + vertex_cnt + 1);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.reserve(edge_cnt);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.clear(), m_low.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, const FlowType &min_cap, FlowType max_cap, CostType cost) {
m_delta[from] -= min_cap, m_delta[to] += min_cap, m_low.push_back(min_cap), m_init_cost += min_cap * cost;
Expand Down Expand Up @@ -254,7 +254,7 @@ namespace OY {
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!vertex_cnt) return;
m_graph.resize(vertex_cnt + 2, edge_cnt + vertex_cnt + 1);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.reserve(edge_cnt);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.clear(), m_low.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType cap, CostType cost) {
if (cap && cost < 0) {
Expand Down Expand Up @@ -301,7 +301,7 @@ namespace OY {
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!vertex_cnt) return;
m_graph.resize(vertex_cnt + 2, edge_cnt + vertex_cnt + 1);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.reserve(edge_cnt);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.clear(), m_low.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType min_cap, FlowType max_cap, CostType cost) {
if (min_cap < max_cap && cost < 0 && from != to) {
Expand Down
2 changes: 1 addition & 1 deletion GRAPH/EdmondsKarp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace OY {
Graph(size_type vertex_cnt = 0, size_type edge_cnt = 0) { resize(vertex_cnt, edge_cnt); }
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!(m_vertex_cnt = vertex_cnt)) return;
m_prepared = false, m_raw_edges.reserve(edge_cnt);
m_prepared = false, m_raw_edges.clear(), m_raw_edges.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType cap) { m_raw_edges.push_back({from, to, cap}); }
template <typename SumType = FlowType>
Expand Down
6 changes: 3 additions & 3 deletions GRAPH/EdmondsKarp_mcmf.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace OY {
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!vertex_cnt) return;
m_graph.resize(vertex_cnt + 2, edge_cnt + vertex_cnt + 1);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.reserve(edge_cnt);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.clear(), m_low.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, const FlowType &min_cap, FlowType max_cap, CostType cost) {
m_delta[from] -= min_cap, m_delta[to] += min_cap, m_low.push_back(min_cap), m_init_cost += min_cap * cost;
Expand Down Expand Up @@ -243,7 +243,7 @@ namespace OY {
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!vertex_cnt) return;
m_graph.resize(vertex_cnt + 2, edge_cnt + vertex_cnt + 1);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.reserve(edge_cnt);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.clear(), m_low.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType cap, CostType cost) {
if (cap && cost < 0) {
Expand Down Expand Up @@ -290,7 +290,7 @@ namespace OY {
void resize(size_type vertex_cnt, size_type edge_cnt) {
if (!vertex_cnt) return;
m_graph.resize(vertex_cnt + 2, edge_cnt + vertex_cnt + 1);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.reserve(edge_cnt);
m_prepared = false, m_init_flow = 0, m_init_cost = 0, m_delta.assign(vertex_cnt, {}), m_low.clear(), m_low.reserve(edge_cnt);
}
void add_edge(size_type from, size_type to, FlowType min_cap, FlowType max_cap, CostType cost) {
if (min_cap < max_cap && cost < 0 && from != to) {
Expand Down
10 changes: 6 additions & 4 deletions STR/ACAutomaton.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
2. [病毒侵袭](https://acm.hdu.edu.cn/showproblem.php?pid=2896)
3. [病毒侵袭持续中](https://acm.hdu.edu.cn/showproblem.php?pid=3065)
4. [2707. 字符串中的额外字符](https://leetcode.cn/problems/extra-characters-in-a-string/)
5. [P2292 [HNOI2004] L 语言](https://www.luogu.com.cn/problem/P2292)
6. [P3796 【模板】AC 自动机(加强版)](https://www.luogu.com.cn/problem/P3796)
7. [P3808 【模板】AC 自动机(简单版)](https://www.luogu.com.cn/problem/P3808)
8. [P5357 【模板】AC 自动机(二次加强版)](https://www.luogu.com.cn/problem/P5357)
5. [3213. 最小代价构造字符串](https://leetcode.cn/problems/construct-string-with-minimum-cost/)
6. [P2292 [HNOI2004] L 语言](https://www.luogu.com.cn/problem/P2292)
7. [P3796 【模板】AC 自动机(加强版)](https://www.luogu.com.cn/problem/P3796)
8. [P3808 【模板】AC 自动机(简单版)](https://www.luogu.com.cn/problem/P3808)
9. [P5357 【模板】AC 自动机(二次加强版)](https://www.luogu.com.cn/problem/P5357)
10. [parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852)



Expand Down
3 changes: 2 additions & 1 deletion TEST/local/HLDBIT_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main() {
cout << T << endl;

// 不妨让每个结点初始值为编号的一万倍
OY::HLDBIT32<decltype(T), 1000> bit(&T, [&](int i) {
OY::HLDBIT32<decltype(T)> bit(&T, [&](int i) {
return i * 10000;
});
cout << bit << endl;
Expand All @@ -38,4 +38,5 @@ int main() {
[30000[10000[0][20000[80000]]][40000][70000[50000][60000]]]
[30100[10100[0][20100[80100]]][40000][70100[50100][60000]]]
[30100[10100[0][20100[80100]]][40000][71100[51100][61000]]]
*/
2 changes: 1 addition & 1 deletion TEST/oj/lc_3182.cpp → TEST/oj/lc_3181.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using namespace std;

/*
[3182. 执行操作可获得的最大总奖励 II](https://leetcode.cn/problems/maximum-total-reward-using-operations-ii/)
[3181. 执行操作可获得的最大总奖励 II](https://leetcode.cn/problems/maximum-total-reward-using-operations-ii/)
*/
/**
* 本题要做的操作是将 bitset 的某个范围的 1 左移,实现背包
Expand Down
58 changes: 58 additions & 0 deletions TEST/oj/lc_3213.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "IO/LeetcodeIO.h"
#include "STR/ACAutomaton.h"
using namespace std;

/*
[3213. 最小代价构造字符串](https://leetcode.cn/problems/construct-string-with-minimum-cost/)
*/
/**
* 本题通过 ac 自动机搜索所有适配位置
*/

class Solution {
public:
int minimumCost(string target, vector<string> &words, vector<int> &costs) {
constexpr uint32_t inf = 0x3f3f3f3f;
struct info {
uint32_t real_fail;
uint16_t len, cost = 0x3f3f;
};
static OY::AC::Automaton<info, 26> S;
S.reserve(50000);
for (int i = 0; i < words.size(); i++) {
auto p = S.get_node(S.insert_lower(words[i]));
p->len = words[i].size();
p->cost = min<int>(p->cost, costs[i]);
}
S.prepare();
// 为了跳过没有匹配的结点,所以预处理一下 real_fail
S.do_for_extending_nodes([](uint32_t cur) {
auto p = S.get_node(cur);
auto fa = S.get_fail_node(cur);
p->real_fail = fa->len ? p->m_fail : fa->real_fail;
});
vector<uint32_t> dp(target.size() + 1, inf);
dp[0] = 0;
uint32_t pos = 0;
for (int r = 0; r < target.size(); r++) {
pos = S.next(pos, target[r] - 'a');
// 遍历所有和 target[~r] 后缀相同的模式串
for (auto x = pos; x; x = S.get_node(x)->real_fail) {
auto p = S.get_node(x);
dp[r + 1] = min(dp[r + 1], dp[r - p->len + 1] + p->cost);
}
}
return dp.back() == inf ? -1 : dp.back();
}
};

#ifdef OY_LOCAL
int main() {
REGISTER_CONSTRUCTOR_SOLUTION;
REGISTER_MEMBERFUNCTION_SOLUTION(minimumCost);
while (true) {
executor.constructSolution();
executor.executeSolution();
}
}
#endif
2 changes: 1 addition & 1 deletion TEST/oj/luogu_p3128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void solve_hldbit() {
}
S.prepare(), S.set_root(0);

OY::HLDBIT::TreeBIT<decltype(S), uint32_t, N> T(&S);
OY::HLDBIT::TreeBIT<decltype(S), uint32_t> T(&S);
for (uint32_t i = 0; i < m; i++) {
uint32_t a, b;
cin >> a >> b;
Expand Down
2 changes: 1 addition & 1 deletion TEST/oj/luogu_p3178.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void solve_hldbit() {
}
S.prepare(), S.set_root(0);

OY::HLDBIT64<decltype(S), N> T(&S, [](uint32_t i) { return val[i]; });
OY::HLDBIT64<decltype(S)> T(&S, [](uint32_t i) { return val[i]; });
for (uint32_t i = 0; i < m; i++) {
char op;
cin >> op;
Expand Down
2 changes: 1 addition & 1 deletion TEST/oj/luogu_p3258.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void solve_hldbit() {
}
S.prepare(), S.set_root(0);

OY::HLDBIT::TreeBIT<decltype(S), uint32_t, N> T(&S);
OY::HLDBIT::TreeBIT<decltype(S), uint32_t> T(&S);
for (uint32_t i = 1; i < n; i++) {
uint32_t a = A[i - 1], b = A[i];
T.add_path<true>(a, b, 1);
Expand Down
2 changes: 1 addition & 1 deletion TEST/oj/luogu_p3384.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void solve_hldbit() {
}
S.prepare(), S.set_root(r - 1);

OY::HLDBIT::TreeBIT<decltype(S), uint64_t, N> T(&S, [&](uint32_t i) { return val[i]; });
OY::HLDBIT::TreeBIT<decltype(S), uint64_t> T(&S, [&](uint32_t i) { return val[i]; });

for (uint32_t i = 0; i < m; i++) {
char op;
Expand Down
2 changes: 1 addition & 1 deletion TEST/oj/luogu_p4211.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void solve_gbt() {

void solve_hldbit() {
using Tree = OY::LinkTree::Tree<bool, N>;
using BIT = OY::HLDBIT::TreeBIT<Tree, uint32_t, N>;
using BIT = OY::HLDBIT::TreeBIT<Tree, uint32_t>;
uint32_t n, m;
cin >> n >> m;
Tree S(n);
Expand Down
2 changes: 1 addition & 1 deletion TEST/oj/luogu_u335992.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main() {
}
S.prepare(), S.set_root(0);

OY::HLDBIT32<decltype(S), N> T(&S);
OY::HLDBIT32<decltype(S)> T(&S);
for (uint32_t i = 0; i < m; i++) {
char op;
cin >> op;
Expand Down
53 changes: 53 additions & 0 deletions TEST/oj/nc_274852.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "DS/LinkBucket.h"
#include "DS/SegCounter.h"
#include "IO/FastIO.h"
#include "STR/ACAutomaton.h"

/*
[parent 树上启发式合并](https://ac.nowcoder.com/acm/problem/274852)
*/
/**
* 注意本题题面坑。询问字符串的和是 300000
*/

int main() {
uint32_t n, q;
std::string s;
cin >> n >> q >> s;

auto get_val = [](char c) {
if (c <= '9')
return c - '0';
else if (c <= 'Z')
return c - 'A' + 10;
else
return c - 'a' + 36;
};
OY::AC::Automaton<OY::AC::BaseNode, 62> ac;
OY::LBC::LinkBucket<std::pair<uint32_t, uint32_t>> qs(300001, q);
OY::StaticSegCounter<uint32_t, uint32_t, true, false, 200000> tails[300001];
for (uint32_t i = 0; i != q; i++) {
std::string s;
uint32_t k;
cin >> s >> k;
auto it = ac.insert(s.begin(), s.end(), [&](char c) { return get_val(c); });
qs[it].push_front(std::make_pair(i, k - 1));
}
ac.prepare();

uint32_t pos = 0;
for (uint32_t i = 0; i < s.size(); i++) {
pos = ac.next(pos, get_val(s[i]));
tails[pos].add_positive(i, 1);
}
std::vector<uint32_t> ans(q);
ac.do_for_failing_nodes([&](uint32_t a) {
// 先解决本地的询问
for (auto &[idx, k] : qs[a]) ans[idx] = k < tails[a].query_all() ? tails[a].kth(k)->key() + 1 : -1;
// 再把本地的尾下标们合并到 fail 上
uint32_t p = ac.query_fail(a);
tails[p].merge(tails[a]);
});
// 输出答案
for (int a : ans) cout << a << endl;
}
Loading

0 comments on commit cd0b24d

Please sign in to comment.