-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data_structures -> structures, add fenwick and fenwick_set + ordered_…
…set test
- Loading branch information
1 parent
4a57282
commit 82e0d01
Showing
30 changed files
with
262 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef CP_ALGO_STRUCTURES_FENWICK_HPP | ||
#define CP_ALGO_STRUCTURES_FENWICK_HPP | ||
#include <cassert> | ||
#include <vector> | ||
#include <bit> | ||
namespace cp_algo::structures { | ||
template<typename T, typename Container = std::vector<T>> | ||
struct fenwick { | ||
size_t n; | ||
Container data; | ||
|
||
fenwick(auto &&range) { | ||
assign(range); | ||
} | ||
void to_prefix_sums() { | ||
for(size_t i = 1; i < n; i++) { | ||
if(i + (i & -i) <= n) { | ||
data[i + (i & -i)] += data[i]; | ||
} | ||
} | ||
} | ||
void assign(auto &&range) { | ||
n = size(range) - 1; | ||
data = move(range); | ||
to_prefix_sums(); | ||
} | ||
void add(size_t x, T const& v) { | ||
for(++x; x <= n; x += x & -x) { | ||
data[x] += v; | ||
} | ||
} | ||
// sum of [0, r) | ||
T prefix_sum(size_t r) const { | ||
assert(r <= n); | ||
T res = 0; | ||
for(; r; r -= r & -r) { | ||
res += data[r]; | ||
} | ||
return res; | ||
} | ||
// sum of [l, r) | ||
T range_sum(size_t l, size_t r) const { | ||
return prefix_sum(r) - prefix_sum(l); | ||
} | ||
// First r s.t. prefix_sum(r) >= k | ||
// Assumes data[x] >= 0 for all x | ||
size_t prefix_lower_bound(T k) const { | ||
int x = 0; | ||
for(size_t i = std::bit_floor(n); i; i /= 2) { | ||
if(x + i <= n && data[x + i] < k) { | ||
k -= data[x + i]; | ||
x += i; | ||
} | ||
} | ||
return x; | ||
} | ||
}; | ||
} | ||
#endif // CP_ALGO_STRUCTURES_FENWICK_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef CP_ALGO_STRUCTURES_FENWICK_SET_HPP | ||
#define CP_ALGO_STRUCTURES_FENWICK_SET_HPP | ||
#include "fenwick.hpp" | ||
#include <bitset> | ||
namespace cp_algo::structures { | ||
// fenwick-based set for [0, maxc) | ||
template<size_t maxc> | ||
struct fenwick_set: fenwick<int, std::array<int, maxc+1>> { | ||
using Base = fenwick<int, std::array<int, maxc+1>>; | ||
size_t sz = 0; | ||
std::bitset<maxc> present; | ||
fenwick_set(): Base(std::array<int, maxc+1>()) {} | ||
fenwick_set(auto &&range): fenwick_set() { | ||
for(auto x: range) { | ||
Base::data[x + 1] = 1; | ||
sz += !present[x]; | ||
present[x] = 1; | ||
} | ||
Base::to_prefix_sums(); | ||
} | ||
void insert(size_t x) { | ||
if(present[x]) return; | ||
present[x] = 1; | ||
sz++; | ||
Base::add(x, 1); | ||
} | ||
void erase(size_t x) { | ||
if(!present[x]) return; | ||
present[x] = 0; | ||
sz--; | ||
Base::add(x, -1); | ||
} | ||
size_t order_of_key(size_t x) const { | ||
return Base::prefix_sum(x); | ||
} | ||
size_t find_by_order(size_t order) const { | ||
return order < sz ? Base::prefix_lower_bound(order + 1) : -1; | ||
} | ||
size_t lower_bound(size_t x) const { | ||
if(present[x]) {return x;} | ||
auto order = order_of_key(x); | ||
return order < sz ? find_by_order(order) : -1; | ||
} | ||
size_t pre_upper_bound(size_t x) const { | ||
if(present[x]) {return x;} | ||
auto order = order_of_key(x); | ||
return order ? find_by_order(order - 1) : -1; | ||
} | ||
}; | ||
} | ||
#endif // CP_ALGO_STRUCTURES_FENWICK_SET_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_BASE_HPP | ||
#define CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_BASE_HPP | ||
namespace cp_algo::structures::segtree::metas { | ||
template<typename derived_meta> | ||
struct base_meta { | ||
using meta = derived_meta; | ||
virtual void pull(meta const&, meta const&, int, int) {}; | ||
virtual void push(meta*, meta*, int, int) {}; | ||
}; | ||
} | ||
#endif // CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_BASE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#ifndef CP_ALGO_STRUCTURES_TREAP_COMMON_HPP | ||
#define CP_ALGO_STRUCTURES_TREAP_COMMON_HPP | ||
#define _safe(t, op) (t ? t->op : typename std::remove_reference_t<decltype(t->op)>()) | ||
#endif // CP_ALGO_STRUCTURES_TREAP_COMMON_HPP |
8 changes: 4 additions & 4 deletions
8
cp-algo/data_structures/treap/metas/base.hpp → cp-algo/structures/treap/metas/base.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
#ifndef CP_ALGO_DATA_STRUCTURES_TREAP_METAS_BASE_HPP | ||
#define CP_ALGO_DATA_STRUCTURES_TREAP_METAS_BASE_HPP | ||
#ifndef CP_ALGO_STRUCTURES_TREAP_METAS_BASE_HPP | ||
#define CP_ALGO_STRUCTURES_TREAP_METAS_BASE_HPP | ||
#include "../common.hpp" | ||
#include <functional> | ||
#include <algorithm> | ||
#include <cstdint> | ||
#define _safe_meta(i, op) _safe(i, _meta.op) | ||
namespace cp_algo::data_structures::treap::metas { | ||
namespace cp_algo::structures::treap::metas { | ||
struct base_meta { | ||
void pull(auto const, auto const){} | ||
void push(auto&, auto&){} | ||
}; | ||
} | ||
#endif // CP_ALGO_DATA_STRUCTURES_TREAP_METAS_BASE_HPP | ||
#endif // CP_ALGO_STRUCTURES_TREAP_METAS_BASE_HPP |
Oops, something went wrong.