Skip to content

Commit 82e0d01

Browse files
committed
data_structures -> structures, add fenwick and fenwick_set + ordered_set test
1 parent 4a57282 commit 82e0d01

30 files changed

+262
-89
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"verify/algebra/poly/convolution107.test.cpp": "2024-02-11 15:34:32 +0100",
44
"verify/algebra/poly/find_linrec.test.cpp": "2024-02-11 15:34:32 +0100",
55
"verify/algebra/poly/poly_sqrt.test.cpp": "2024-02-11 15:34:32 +0100",
6-
"verify/data_structures/segtree/range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
7-
"verify/data_structures/segtree/range_chmin_chmax_add_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
8-
"verify/data_structures/treap/cartesian_tree.test.cpp": "2024-02-11 15:34:32 +0100",
9-
"verify/data_structures/treap/dynamic_sequence_range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
10-
"verify/data_structures/treap/range_reverse_range_sum.test.cpp": "2024-02-11 15:34:32 +0100"
6+
"verify/structures/segtree/range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
7+
"verify/structures/segtree/range_chmin_chmax_add_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
8+
"verify/structures/treap/cartesian_tree.test.cpp": "2024-02-11 15:34:32 +0100",
9+
"verify/structures/treap/dynamic_sequence_range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
10+
"verify/structures/treap/range_reverse_range_sum.test.cpp": "2024-02-11 15:34:32 +0100"
1111
}

cp-algo/data_structures/segtree/metas/base.hpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

cp-algo/data_structures/treap/common.hpp

Lines changed: 0 additions & 4 deletions
This file was deleted.

cp-algo/graph/base.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CP_ALGO_GRAPH_BASE_HPP
22
#define CP_ALGO_GRAPH_BASE_HPP
33
#include "edge_types.hpp"
4-
#include "../data_structures/stack_union.hpp"
4+
#include "../structures/stack_union.hpp"
55
#include <ranges>
66
#include <vector>
77
namespace cp_algo::graph {
@@ -56,7 +56,7 @@ namespace cp_algo::graph {
5656
private:
5757
node_index v0;
5858
std::vector<edge_t> edges;
59-
data_structures::stack_union<edge_index> adj;
59+
structures::stack_union<edge_index> adj;
6060
};
6161
}
6262
#endif // CP_ALGO_GRAPH_BASE_HPP

cp-algo/graph/mst.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CP_ALGO_GRAPH_MST_HPP
22
#define CP_ALGO_GRAPH_MST_HPP
33
#include "base.hpp"
4-
#include "../data_structures/dsu.hpp"
4+
#include "../structures/dsu.hpp"
55
#include <algorithm>
66
namespace cp_algo::graph {
77
template<weighted_edge_type edge_t>
@@ -11,7 +11,7 @@ namespace cp_algo::graph {
1111
edges.emplace_back(g.edge(e).w, e);
1212
});
1313
std::ranges::sort(edges);
14-
data_structures::dsu me(g.n());
14+
structures::dsu me(g.n());
1515
int64_t total = 0;
1616
std::vector<edge_index> mst;
1717
for(auto [w, e]: edges) {

cp-algo/data_structures/bitpack.hpp renamed to cp-algo/structures/bitpack.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
#ifndef CP_ALGO_DATA_STRUCTURES_BITPACK_HPP
3-
#define CP_ALGO_DATA_STRUCTURES_BITPACK_HPP
2+
#ifndef CP_ALGO_STRUCTURES_BITPACK_HPP
3+
#define CP_ALGO_STRUCTURES_BITPACK_HPP
44
#include <cstdint>
55
#include <cstddef>
66
#include <string>
77
#include <array>
88
#include <bit>
9-
namespace cp_algo::data_structures {
9+
namespace cp_algo::structures {
1010
template<size_t n, typename Int = uint64_t>
1111
struct bitpack {
1212
static constexpr uint8_t bits_per_block = 8 * sizeof(Int);
@@ -76,4 +76,4 @@ namespace cp_algo::data_structures {
7676
}
7777
};
7878
}
79-
#endif // CP_ALGO_DATA_STRUCTURES_BITPACK_HPP
79+
#endif // CP_ALGO_STRUCTURES_BITPACK_HPP

cp-algo/data_structures/dsu.hpp renamed to cp-algo/structures/dsu.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#ifndef CP_ALGO_DATA_STRUCTURES_DSU_HPP
2-
#define CP_ALGO_DATA_STRUCTURES_DSU_HPP
1+
#ifndef CP_ALGO_STRUCTURES_DSU_HPP
2+
#define CP_ALGO_STRUCTURES_DSU_HPP
33
#include <numeric>
44
#include <vector>
5-
namespace cp_algo::data_structures {
5+
namespace cp_algo::structures {
66
struct disjoint_set_union {
77
disjoint_set_union (int n): par(n) {
88
std::iota(begin(par), end(par), 0);
@@ -21,4 +21,4 @@ namespace cp_algo::data_structures {
2121
};
2222
using dsu = disjoint_set_union;
2323
}
24-
#endif // CP_ALGO_DATA_STRUCTURES_DSU_HPP
24+
#endif // CP_ALGO_STRUCTURES_DSU_HPP

cp-algo/data_structures/eertree.hpp renamed to cp-algo/structures/eertree.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#ifndef CP_ALGO_DATA_STRUCTURES_EERTREE_HPP
2-
#define CP_ALGO_DATA_STRUCTURES_EERTREE_HPP
1+
#ifndef CP_ALGO_STRUCTURES_EERTREE_HPP
2+
#define CP_ALGO_STRUCTURES_EERTREE_HPP
33
#include <forward_list>
44
#include <functional>
55
#include <iostream>
66
#include <vector>
77
#include <string>
88
#include "../bump_alloc.hpp"
9-
namespace cp_algo::data_structures {
9+
namespace cp_algo::structures {
1010
template<int sigma = 26, char mch = 'a'>
1111
struct eertree {
1212
eertree(size_t q) {
@@ -69,4 +69,4 @@ namespace cp_algo::data_structures {
6969
int n = 1, sz = 2, last = 0;
7070
};
7171
}
72-
#endif // CP_ALGO_DATA_STRUCTURES_EERTREE_HPP
72+
#endif // CP_ALGO_STRUCTURES_EERTREE_HPP

cp-algo/structures/fenwick.hpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef CP_ALGO_STRUCTURES_FENWICK_HPP
2+
#define CP_ALGO_STRUCTURES_FENWICK_HPP
3+
#include <cassert>
4+
#include <vector>
5+
#include <bit>
6+
namespace cp_algo::structures {
7+
template<typename T, typename Container = std::vector<T>>
8+
struct fenwick {
9+
size_t n;
10+
Container data;
11+
12+
fenwick(auto &&range) {
13+
assign(range);
14+
}
15+
void to_prefix_sums() {
16+
for(size_t i = 1; i < n; i++) {
17+
if(i + (i & -i) <= n) {
18+
data[i + (i & -i)] += data[i];
19+
}
20+
}
21+
}
22+
void assign(auto &&range) {
23+
n = size(range) - 1;
24+
data = move(range);
25+
to_prefix_sums();
26+
}
27+
void add(size_t x, T const& v) {
28+
for(++x; x <= n; x += x & -x) {
29+
data[x] += v;
30+
}
31+
}
32+
// sum of [0, r)
33+
T prefix_sum(size_t r) const {
34+
assert(r <= n);
35+
T res = 0;
36+
for(; r; r -= r & -r) {
37+
res += data[r];
38+
}
39+
return res;
40+
}
41+
// sum of [l, r)
42+
T range_sum(size_t l, size_t r) const {
43+
return prefix_sum(r) - prefix_sum(l);
44+
}
45+
// First r s.t. prefix_sum(r) >= k
46+
// Assumes data[x] >= 0 for all x
47+
size_t prefix_lower_bound(T k) const {
48+
int x = 0;
49+
for(size_t i = std::bit_floor(n); i; i /= 2) {
50+
if(x + i <= n && data[x + i] < k) {
51+
k -= data[x + i];
52+
x += i;
53+
}
54+
}
55+
return x;
56+
}
57+
};
58+
}
59+
#endif // CP_ALGO_STRUCTURES_FENWICK_HPP

cp-algo/structures/fenwick_set.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef CP_ALGO_STRUCTURES_FENWICK_SET_HPP
2+
#define CP_ALGO_STRUCTURES_FENWICK_SET_HPP
3+
#include "fenwick.hpp"
4+
#include <bitset>
5+
namespace cp_algo::structures {
6+
// fenwick-based set for [0, maxc)
7+
template<size_t maxc>
8+
struct fenwick_set: fenwick<int, std::array<int, maxc+1>> {
9+
using Base = fenwick<int, std::array<int, maxc+1>>;
10+
size_t sz = 0;
11+
std::bitset<maxc> present;
12+
fenwick_set(): Base(std::array<int, maxc+1>()) {}
13+
fenwick_set(auto &&range): fenwick_set() {
14+
for(auto x: range) {
15+
Base::data[x + 1] = 1;
16+
sz += !present[x];
17+
present[x] = 1;
18+
}
19+
Base::to_prefix_sums();
20+
}
21+
void insert(size_t x) {
22+
if(present[x]) return;
23+
present[x] = 1;
24+
sz++;
25+
Base::add(x, 1);
26+
}
27+
void erase(size_t x) {
28+
if(!present[x]) return;
29+
present[x] = 0;
30+
sz--;
31+
Base::add(x, -1);
32+
}
33+
size_t order_of_key(size_t x) const {
34+
return Base::prefix_sum(x);
35+
}
36+
size_t find_by_order(size_t order) const {
37+
return order < sz ? Base::prefix_lower_bound(order + 1) : -1;
38+
}
39+
size_t lower_bound(size_t x) const {
40+
if(present[x]) {return x;}
41+
auto order = order_of_key(x);
42+
return order < sz ? find_by_order(order) : -1;
43+
}
44+
size_t pre_upper_bound(size_t x) const {
45+
if(present[x]) {return x;}
46+
auto order = order_of_key(x);
47+
return order ? find_by_order(order - 1) : -1;
48+
}
49+
};
50+
}
51+
#endif // CP_ALGO_STRUCTURES_FENWICK_SET_HPP

0 commit comments

Comments
 (0)