跳到正文

个人记录

一坨bitset

Phartial约 21 分钟
原文
文章信息与版本

文章信息

收录
2026年1月8日
更新
2026年1月10日
篇幅
约 21 分钟

版本

  1. v1当前

经过不充分也不必要的校验,成功证明了这坨东西比正常手写还慢!!!

有没有大神愿意教我常数优化的!!!

C++
#include <algorithm>
#include <cstring>
#include <vector>

#define UNROLL2(op, i, j) \
  op(i, j##0);            \
  op(i, j##1)
#define UNROLL4(op, i, j) \
  UNROLL2(op, i, j##0);   \
  UNROLL2(op, i, j##1)
#define UNROLL8(op, i) \
  UNROLL4(op, i, 0b0); \
  UNROLL4(op, i, 0b1)
#define UNROLL7(op, i)  \
  UNROLL4(op, i, 0b0);  \
  UNROLL2(op, i, 0b10); \
  op(i, 0b110)
#define CASE(op, i, j) \
  case (j + 1):        \
    op(i, j);
#define CASE2(op, i, j) CASE(op, i, j##1) CASE(op, i, j##0)
#define CASE4(op, i, j) CASE2(op, i, j##1) CASE2(op, i, j##0)
#define CASE7(op, i) CASE(op, i, 0b110) CASE2(op, i, 0b10) CASE4(op, i, 0b0)
#define CASE6(op, i) CASE2(op, i, 0b10) CASE4(op, i, 0b0)

using uint = unsigned int;
using ull = unsigned long long;

constexpr uint kW = 64;

uint _popcnt(ull n) {
  n = (n & 0xF0F0F0F0F0F0F0F) + (n >> 4 & 0xF0F0F0F0F0F0F0F);
  return n * 0x101010101010101 >> 56;
}
__attribute__((always_inline)) inline void assign(ull* __restrict__ d, uint di, const ull* __restrict__ s, uint si) {
  if (s[si / kW] >> (si % kW) & 1) {
    d[di / kW] |= 1ull << (di % kW);
  } else {
    d[di / kW] &= ~(1ull << (di % kW));
  }
}
void bitcpy(ull* __restrict__ d, uint di, const ull* __restrict__ s, uint si, uint len) {  // from 宋佳兴《论现代硬件上的常数优化》, 2024年国家集训队论文集
  while (len && di % kW) assign(d, di++, s, si++), len--;
  auto dst = d + di / kW;
  auto src = s + si / kW;
  auto end = dst + len / kW;
  auto shift = si % kW;
  if (!shift)
    memcpy(dst, src, (end - dst) * 8);
  else
    while (dst != end) *dst++ = src[0] >> shift | src[1] << kW - shift, src++;
  di += len & -kW, si += len & -kW, len %= kW;
  while (len--) assign(d, di++, s, si++);
}
struct bitset {
  uint n, na;
  std::vector<ull> a;

  bitset() {}
  bitset(uint _n) { resize(_n); }
  void resize(uint _n) { a.resize(na = (_n + kW - 1) / kW), n = _n; }
  static bitset make_from_ull(ull v) {
    bitset x = bitset(64);
    x.a[0] = v;
    return x;
  }
  static bitset make_from_string(std::string s) {
    bitset x = bitset(s.size());
    for (uint i = 0; i < s.size(); ++i) {
      if (s[i] == '1') {
        x.set(i);
      }
    }
    return x;
  }
  __attribute__((always_inline)) inline bool test(uint i) const { return a[i / kW] >> (i % kW) & 1; }
  __attribute__((always_inline)) inline void set(uint i) { a[i / kW] |= 1ull << (i % kW); }
  __attribute__((always_inline)) inline void reset(uint i) { a[i / kW] &= ~(1ull << (i % kW)); }
  __attribute__((always_inline)) inline void set(uint i, bool v) { v ? set(i) : reset(i); }
  __attribute__((always_inline)) inline void flip(uint i) { a[i / kW] ^= 1ull << (i % kW); }
  void set() { memset(a.data(), -1, na * 8); }
  void reset() { memset(a.data(), 0, na * 8); }
  void flip() {
#define op(i, j) a[i + j] = ~a[i + j]
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    switch (na - i) {
      CASE7(op, i)
    }
#undef op
  }
  bool all() const {
#define init(i, j) ull s##j = -1
#define op(i, j) s##j &= a[i + j]
    UNROLL8(init, 0);
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    switch (na - i) {
      CASE7(op, i);
    }
    return (s0b000 & s0b001 & s0b010 & s0b011 & s0b100 & s0b101 & s0b110 & s0b111) == -1;
#undef init
#undef op
  }
  bool any() const {
#define init(i, j) ull s##j = 0
#define op(i, j) s##j |= a[i + j]
    UNROLL8(init, 0);
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    switch (na - i) {
      CASE7(op, i);
    }
    return (s0b000 | s0b001 | s0b010 | s0b011 | s0b100 | s0b101 | s0b110 | s0b111);
  }
  bool none() const {
    UNROLL8(init, 0);
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    switch (na - i) {
      CASE7(op, i);
    }
    return (s0b000 | s0b001 | s0b010 | s0b011 | s0b100 | s0b101 | s0b110 | s0b111) == 0;
#undef init
#undef op
  }
  uint count() const {
#define op(i, j) ull n##j, low##j, high##j
    UNROLL7(op, 0);
#undef op
#define init(i, j) low##j = 0, high##j = 0
#define op(i, j)                          \
  n##j = a[i + j];                        \
  n##j -= n##j >> 1 & 0x5555555555555555; \
  low##j = n##j & 0x3333333333333333;     \
  high##j = n##j >> 2 & 0x3333333333333333
    uint s = 0, i = 0;
    for (; i + 6 < na; i += 7) {
      UNROLL7(init, i);
      UNROLL7(op, i);
      s += _popcnt(low0b000 + low0b001 + low0b010 + low0b011 + low0b100 + low0b101 + low0b110) + _popcnt(high0b000 + high0b001 + high0b010 + high0b011 + high0b100 + high0b101 + high0b110);
    }
    UNROLL7(init, i);
    switch (na - i) {
      CASE6(op, i)
    }
    s += _popcnt(low0b000 + low0b001 + low0b010 + low0b011 + low0b100 + low0b101 + low0b110) + _popcnt(high0b000 + high0b001 + high0b010 + high0b011 + high0b100 + high0b101 + high0b110);
    return s;
#undef init
#undef op
  }
  uint find_first() const {
    auto i = find_if(a.begin(), a.end(), [](ull i) { return i; });
    return i == a.end() ? -1 : ((i - a.begin()) * kW | __builtin_ctzll(*i));
  }
  uint find_last() const {
    auto i = find_if(a.rbegin(), a.rend(), [](ull i) { return i; });
    return i == a.rend() ? -1 : ((a.rend() - i) * kW - 1 ^ __builtin_clzll(*i));
  }
  uint find_next(uint i) const {
    uint b = i / kW;
    if (a[b] && __builtin_ctzll(a[b]) > (i % kW)) {
      return b * kW | __builtin_ctzll(a[b]);
    }
    auto j = find_if(a.begin() + b + 1, a.end(), [](ull i) { return i; });
    return j == a.end() ? -1 : ((j - a.begin()) * kW | __builtin_ctzll(*j));
  }
  uint find_prev(uint i) const {
    uint b = i / kW;
    if (a[b] && (kW - 1 ^ __builtin_clzll(a[b])) < (i % kW)) {
      return b * kW | (kW - 1 ^ __builtin_clzll(a[b]));
    }
    auto j = find_if(a.rend() - b, a.rend(), [](ull i) { return i; });
    return j == a.rend() ? -1 : ((a.rend() - j) * kW - 1 ^ __builtin_clzll(*j));
  }
#define def_binary_oper(op)                                                   \
  bitset operator op(const bitset& o) const {                                 \
    bitset s(n);                                                              \
    auto oper = [&](uint i, uint j) { s.a[i + j] = a[i + j] op o.a[i + j]; }; \
    uint i = 0;                                                               \
    for (; i + 7 < na; i += 8) {                                              \
      UNROLL8(oper, i);                                                       \
    }                                                                         \
    switch (na - i) {                                                         \
      CASE7(oper, i)                                                          \
    }                                                                         \
    return s;                                                                 \
  }                                                                           \
  bitset& operator op## = (const bitset& o) {                                 \
    auto oper = [&](uint i, uint j) { a[i + j] op## = o.a[i + j]; };          \
    uint i = 0;                                                               \
    for (; i + 7 < na; i += 8) {                                              \
      UNROLL8(oper, i);                                                       \
    }                                                                         \
    switch (na - i) {                                                         \
      CASE7(oper, i)                                                          \
    }                                                                         \
    return *this;                                                             \
  }
  def_binary_oper(|)
      def_binary_oper(&)
          def_binary_oper(^)
#undef def_binary_oper

              bitset operator~() const {
#define op(i, j) s.a[i + j] = ~a[i + j]
    bitset s(n);
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    switch (na - i) {
      CASE7(op, i)
    }
    return s;
#undef op
  }
  bitset operator<<(uint l) const {
    bitset s(n);
    bitcpy(s.a.data(), l, a.data(), 0, n - l);
    return s;
  }
  bitset& operator<<=(uint l) {
    bitset s = *this << l;
    a = move(s.a);
    return *this;
  }
  bitset operator>>(uint l) const {
    bitset s(n);
    bitcpy(s.a.data(), 0, a.data(), l, n - l);
    return s;
  }
  bitset& operator>>=(uint l) {
    bitset s = *this >> l;
    a = move(s.a);
    return *this;
  }
  bitset operator+(const bitset& o) const {
#define op(i, j) p = __builtin_ia32_addcarryx_u64(p, a[i + j], o.a[i + j], s.a.data() + i + j)
    bitset s(n);
    uint i = 0;
    unsigned char p = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    for (; i < na; ++i) {
      op(i, 0);
    }
    return s;
  }
  bitset add_with_carry(const bitset& o, unsigned char p) const {
    bitset s(n);
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    for (; i < na; ++i) {
      op(i, 0);
    }
    return s;
#undef op
  }
  bitset operator-(const bitset& o) const {
#define op(i, j) p = __builtin_ia32_sbb_u64(p, a[i + j], o.a[i + j], s.a.data() + i + j)
    bitset s(n);
    uint i = 0;
    unsigned char p = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    for (; i < na; ++i) {
      op(i, 0);
    }
    return s;
  }
  bitset sub_with_borrow(const bitset& o, unsigned char p) const {
    bitset s(n);
    uint i = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    for (; i < na; ++i) {
      op(i, 0);
    }
    return s;
#undef op
  }
  bitset& operator+=(const bitset& o) {
#define op(i, j) p = __builtin_ia32_addcarryx_u64(p, a[i + j], o.a[i + j], a.data() + i + j)
    uint i = 0;
    unsigned char p = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    for (; i < na; ++i) {
      op(i, 0);
    }
    return *this;
#undef op
  }
  bitset& operator-=(const bitset& o) {
#define op(i, j) p = __builtin_ia32_sbb_u64(p, a[i + j], o.a[i + j], a.data() + i + j)
    uint i = 0;
    unsigned char p = 0;
    for (; i + 7 < na; i += 8) {
      UNROLL8(op, i);
    }
    for (; i < na; ++i) {
      op(i, 0);
    }
    return *this;
#undef op
  }
};
std::ostream& operator<<(std::ostream& out, const bitset& x) {
  for (int i = 0; i < x.n; ++i) {
    out << x.test(i);
  }
  return out;
}
原文