Skip to content

Commit 3235372

Browse files
committed
bitpack fixes
1 parent 7e2e034 commit 3235372

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

cp-algo/structures/bit_array.hpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,31 @@
22
#define CP_ALGO_STRUCTURES_BIT_ARRAY_HPP
33
#include "../util/bit.hpp"
44
#include "../util/bump_alloc.hpp"
5+
#include <cassert>
56
namespace cp_algo::structures {
7+
template<typename C>
8+
concept Resizable = requires(C& c, std::size_t n) { c.resize(n); };
9+
610
template<class Cont>
711
struct _bit_array {
812
static constexpr size_t width = bit_width<uint64_t>;
913
size_t words, n;
1014
alignas(32) Cont data;
1115

12-
_bit_array(): words(0), n(0) {}
13-
_bit_array(size_t N): words((N + width - 1) / width), n(N), data() {}
16+
void resize(size_t N) {
17+
n = N;
18+
words = (n + width - 1) / width;
19+
if constexpr (Resizable<Cont>) {
20+
data.resize(words);
21+
} else {
22+
assert(std::size(data) >= words);
23+
}
24+
}
25+
26+
_bit_array(): n(0), words(0), data() {}
27+
_bit_array(size_t N): data() {
28+
resize(N);
29+
}
1430

1531
uint64_t& word(size_t x) {
1632
return data[x];
@@ -47,9 +63,7 @@ namespace cp_algo::structures {
4763
struct bit_array: _bit_array<std::array<uint64_t, (N + 63) / 64>> {
4864
using Base = _bit_array<std::array<uint64_t, (N + 63) / 64>>;
4965
using Base::Base, Base::words, Base::data;
50-
bit_array(): Base(N) {
51-
data.fill(0);
52-
}
66+
bit_array(): Base(N) {}
5367
};
5468
struct dynamic_bit_array: _bit_array<std::vector<uint64_t>> {
5569
using Base = _bit_array<std::vector<uint64_t>>;

cp-algo/structures/fenwick_set.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
#include "fenwick.hpp"
44
#include "bit_array.hpp"
55
namespace cp_algo::structures {
6-
template<size_t maxc, typename Uint = uint64_t>
7-
using popcount_array = std::array<int, maxc / bit_width<Uint> + 1>;
6+
template<size_t maxc>
7+
using popcount_array = std::array<int, maxc / bit_width<uint64_t> + 1>;
88
// fenwick-based set for [0, maxc)
9-
template<size_t maxc, typename Uint = uint64_t>
10-
struct fenwick_set: fenwick<int, popcount_array<maxc, Uint>> {
11-
using Base = fenwick<int, popcount_array<maxc, Uint>>;
12-
static constexpr size_t word = bit_width<Uint>;
9+
template<size_t maxc>
10+
struct fenwick_set: fenwick<int, popcount_array<maxc>> {
11+
using Base = fenwick<int, popcount_array<maxc>>;
12+
static constexpr size_t word = bit_width<uint64_t>;
1313
size_t sz = 0;
14-
bit_array<maxc, Uint> bits;
14+
bit_array<maxc> bits;
1515

16-
fenwick_set(): Base(popcount_array<maxc, Uint>{}) {}
16+
fenwick_set(): Base(popcount_array<maxc>{}) {}
1717
fenwick_set(auto &&range): fenwick_set() {
1818
for(auto x: range) {
1919
Base::data[x / word + 1] += 1;

verify/structures/bitpack/det_mod_2.test.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// @brief Determinant of Matrix (Mod 2)
33
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det_mod_2"
44
#pragma GCC optimize("Ofast,unroll-loops")
5-
#pragma GCC target("tune=native")
65
#include "cp-algo/structures/bitpack.hpp"
76
#include <bits/stdc++.h>
87

@@ -13,20 +12,20 @@ const int maxn = 1 << 12;
1312
bitpack<maxn> a[maxn];
1413

1514
void solve() {
16-
int n;
15+
size_t n;
1716
cin >> n;
1817
string row;
1918
vector<size_t> lead(n);
20-
for(int i = 0; i < n; i++) {
19+
for(size_t i = 0; i < n; i++) {
2120
cin >> row;
2221
a[i] = row;
23-
for(int j = 0; j < i; j++) {
22+
for(size_t j = 0; j < i; j++) {
2423
if(a[i][lead[j]]) {
2524
a[i].xor_hint(a[j], lead[j]);
2625
}
2726
}
2827
lead[i] = a[i].ctz();
29-
if(lead[i] == maxn) {
28+
if(lead[i] == n) {
3029
cout << 0 << "\n";
3130
return;
3231
}

verify/structures/bitpack/inv_mod_2.test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using namespace std;
88
using namespace cp_algo::structures;
99

10-
const int maxn = 1 << 13;
11-
bitpack<maxn> a[maxn];
10+
const int maxn = 1 << 12;
11+
bitpack<2 * maxn> a[maxn];
1212

1313
void solve() {
1414
size_t n;
@@ -18,6 +18,7 @@ void solve() {
1818
for(size_t i = 0; i < n; i++) {
1919
cin >> row;
2020
a[i] = row;
21+
a[i].resize(2 * n);
2122
a[i].set(n + i);
2223
for(size_t j = 0; j < i; j++) {
2324
if(a[i][lead[j]]) {

0 commit comments

Comments
 (0)