|
1 | 1 | #ifndef CP_ALGO_STRUCTURES_FENWICK_SET_HPP
|
2 | 2 | #define CP_ALGO_STRUCTURES_FENWICK_SET_HPP
|
3 | 3 | #include "fenwick.hpp"
|
| 4 | +#include <immintrin.h> |
| 5 | +#include <cstdint> |
4 | 6 | #include <bitset>
|
5 | 7 | namespace cp_algo::structures {
|
6 | 8 | // 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>>; |
| 9 | + // Requires GCC target("popcnt,bmi2") |
| 10 | + template<typename Uint> |
| 11 | + constexpr size_t width = sizeof(Uint) * 8; |
| 12 | + template<size_t maxc, typename Uint> |
| 13 | + using popcount_array = std::array<int, maxc / width<Uint> + 1>; |
| 14 | + template<size_t maxc, typename Uint = uint64_t> |
| 15 | + struct fenwick_set: fenwick<int, popcount_array<maxc, Uint>> { |
| 16 | + using Base = fenwick<int, popcount_array<maxc, Uint>>; |
| 17 | + static constexpr size_t word = width<Uint>; |
10 | 18 | size_t sz = 0;
|
11 |
| - std::bitset<maxc> present; |
12 |
| - fenwick_set(): Base(std::array<int, maxc+1>()) {} |
| 19 | + std::array<Uint, maxc / word + 1> bits; |
| 20 | + |
| 21 | + void flip_bit(size_t x) { |
| 22 | + bits[x / word] ^= 1ULL << (x % word); |
| 23 | + } |
| 24 | + bool present(size_t x) const { |
| 25 | + return (bits[x / word] >> (x % word)) & 1; |
| 26 | + } |
| 27 | + |
| 28 | + fenwick_set(): Base(popcount_array<maxc, Uint>{}) {} |
13 | 29 | fenwick_set(auto &&range): fenwick_set() {
|
14 | 30 | for(auto x: range) {
|
15 |
| - Base::data[x + 1] = 1; |
16 |
| - sz += !present[x]; |
17 |
| - present[x] = 1; |
| 31 | + Base::data[x / word + 1] += 1; |
| 32 | + if(!present(x)) { |
| 33 | + sz++; |
| 34 | + flip_bit(x); |
| 35 | + } |
18 | 36 | }
|
19 | 37 | Base::to_prefix_sums();
|
20 | 38 | }
|
21 | 39 | void insert(size_t x) {
|
22 |
| - if(present[x]) return; |
23 |
| - present[x] = 1; |
| 40 | + if(present(x)) return; |
| 41 | + flip_bit(x); |
24 | 42 | sz++;
|
25 |
| - Base::add(x, 1); |
| 43 | + Base::add(x / word, 1); |
26 | 44 | }
|
27 | 45 | void erase(size_t x) {
|
28 |
| - if(!present[x]) return; |
29 |
| - present[x] = 0; |
| 46 | + if(!present(x)) return; |
| 47 | + flip_bit(x); |
30 | 48 | sz--;
|
31 |
| - Base::add(x, -1); |
| 49 | + Base::add(x / word, -1); |
| 50 | + } |
| 51 | + static size_t order_of_bit(Uint x, size_t k) { |
| 52 | + return k ? std::popcount(x << (word - k)) : 0; |
32 | 53 | }
|
33 | 54 | size_t order_of_key(size_t x) const {
|
34 |
| - return Base::prefix_sum(x); |
| 55 | + return Base::prefix_sum(x / word) + order_of_bit(bits[x / word], x % word); |
| 56 | + } |
| 57 | + static size_t kth_set_bit(Uint x, size_t k) { |
| 58 | + return std::countr_zero(_pdep_u64(1ULL << k, x)); |
35 | 59 | }
|
36 | 60 | size_t find_by_order(size_t order) const {
|
37 |
| - return order < sz ? Base::prefix_lower_bound(order + 1) : -1; |
| 61 | + if(order >= sz) { |
| 62 | + return -1; |
| 63 | + } |
| 64 | + auto [x, remainder] = Base::prefix_lower_bound(order + 1); |
| 65 | + return x * word + kth_set_bit(bits[x], remainder - 1); |
38 | 66 | }
|
39 | 67 | size_t lower_bound(size_t x) const {
|
40 |
| - if(present[x]) {return x;} |
| 68 | + if(present(x)) {return x;} |
41 | 69 | auto order = order_of_key(x);
|
42 | 70 | return order < sz ? find_by_order(order) : -1;
|
43 | 71 | }
|
44 | 72 | size_t pre_upper_bound(size_t x) const {
|
45 |
| - if(present[x]) {return x;} |
| 73 | + if(present(x)) {return x;} |
46 | 74 | auto order = order_of_key(x);
|
47 | 75 | return order ? find_by_order(order - 1) : -1;
|
48 | 76 | }
|
|
0 commit comments