This documentation is automatically generated by online-judge-tools/verification-helper
#include "setfunc/boolean_range_add_point_get.hpp"// Source: https://github.com/maspypy/library/blob/main/setfunc/boolean_range_add_point_get.hpp
#include "random/shuffle.hpp"
#include "alg/monoid/add.hpp"
#include "alg/monoid/xor.hpp"
#include "enumerate/bits.hpp"
// O((4/3)^LOG) per query
template <typename Monoid>
struct Boolean_Range_Add_Point_Get {
using MX = Monoid;
using X = typename MX::value_type;
const int LOG;
vc<X> S;
array<u32, 3> mask;
/*
0: [x0,x1]
1: [x0+x1,x0]
2: [x0+x1,x1]
*/
Boolean_Range_Add_Point_Get(int LOG) : LOG(LOG), mask{} {
S.assign(1 << LOG, MX::id());
init_by_random();
}
void init_by_random() {
mask[0] = mask[1] = mask[2] = 0;
FOR(i, LOG) { mask[RNG(0, 3)] |= u32(1) << i; }
}
void init_by_query(const vc<pair<u32, u32>>& ADD, const vc<u32>& GET) {
for (auto& [lo, hi] : ADD) assert((lo & ~hi) == 0);
mask[0] = mask[1] = mask[2] = 0;
auto eval = [&]() -> ll {
ll ans = 0;
for (auto& [lo, hi] : ADD) {
u32 s = 0;
s ^= (lo ^ hi) & mask[0];
s ^= lo & mask[1];
s ^= (~hi) & mask[2];
ans += 1 << popcnt(s);
}
for (u32 i : GET) {
u32 s = 0;
s ^= (~i) & mask[1];
s ^= i & mask[2];
ans += 1 << popcnt(s);
}
return ans;
};
vc<int> I(LOG);
FOR(i, LOG) I[i] = i;
shuffle(I);
array<ll, 3> c;
for (int i : I) {
FOR(k, 3) {
mask[k] |= u32(1) << i, c[k] = eval(), mask[k] &= ~(u32(1) << i);
}
int k = min_element(all(c)) - c.begin();
mask[k] |= u32(1) << i;
}
}
void add(u32 lo, u32 hi, X x) {
assert((lo & ~hi) == 0);
u32 a = 0;
u32 s = 0;
u32 b = 0;
a ^= lo & mask[0];
s ^= (lo ^ hi) & mask[0];
a ^= (~hi) & mask[1];
s ^= lo & mask[1];
b ^= lo & mask[1];
a ^= lo & mask[2];
s ^= (~hi) & mask[2];
b ^= (~hi) & mask[2];
enumerate_all_subset<u32, true>(s, [&](u32 t) -> void {
X y = (__builtin_parity(t & b) ? MX::inverse(x) : x);
S[a | t] = MX::op(S[a | t], y);
});
}
X get(u32 i) {
u32 a = i & mask[0];
u32 s = 0;
s ^= (~i) & mask[1];
s ^= i & mask[2];
X ANS = MX::id();
enumerate_all_subset<u32, true>(
s, [&](u32 t) -> void { ANS = MX::op(ANS, S[a | t]); });
return ANS;
}
};#line 1 "setfunc/boolean_range_add_point_get.hpp"
// Source: https://github.com/maspypy/library/blob/main/setfunc/boolean_range_add_point_get.hpp
#line 1 "random/shuffle.hpp"
// Source: https://github.com/maspypy/library/blob/main/random/shuffle.hpp
#line 1 "random/base.hpp"
// Source: https://github.com/maspypy/library/blob/main/random/base.hpp
u64 RNG_64() {
static u64 x_ = u64(chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count()) *
10150724397891781847ULL;
x_ ^= x_ << 7;
return x_ ^= x_ >> 9;
}
u64 RNG(u64 lim) {
assert(lim > 0);
return RNG_64() % lim;
}
ll RNG(ll l, ll r) {
assert(l < r);
return l + RNG_64() % (r - l);
}
#line 3 "random/shuffle.hpp"
template <typename T>
void shuffle(vc<T>& A) {
FOR(i, len(A)) {
int j = RNG(0, i + 1);
if (i != j) swap(A[i], A[j]);
}
}
#line 1 "alg/monoid/add.hpp"
// Source: https://github.com/maspypy/library/blob/main/alg/monoid/add.hpp
template <typename E>
struct Monoid_Add {
using X = E;
using value_type = X;
static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
static constexpr X inverse(const X &x) noexcept { return -x; }
static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; }
static constexpr X id() { return X(0); }
static constexpr bool commute = true;
};
#line 1 "alg/monoid/xor.hpp"
// Source: https://github.com/maspypy/library/blob/main/alg/monoid/xor.hpp
template <typename X>
struct Monoid_Xor {
using value_type = X;
static X op(X x, X y) { return x ^ y; }
static constexpr X inverse(const X &x) noexcept { return x; }
static constexpr X power(const X &x, ll n) noexcept {
return (n & 1 ? x : 0);
}
static constexpr X id(){return X(0);};
static constexpr bool commute = true;
};
#line 1 "enumerate/bits.hpp"
// Source: https://github.com/maspypy/library/blob/main/enumerate/bits.hpp
template <typename BS, typename F>
void enumerate_bits_bitset(BS& b, int L, int R, F&& f) {
if (L >= len(b)) return;
bool end = false;
int p = (b[L] ? L : b._Find_next(L));
while (p < R) {
f(p, end);
if (end) break;
p = b._Find_next(p);
}
}
template <typename UINT, typename F>
inline void enumerate_all_bit(UINT s, F&& f) {
static_assert(is_unsigned<UINT>::value);
bool end = false;
while (s) {
f(lowbit(s), end);
if (end) break;
s &= s - 1;
}
}
template <typename UINT, bool inc_empty, typename F>
inline void enumerate_all_subset(UINT s, F&& f) {
static_assert(is_unsigned<UINT>::value);
bool end = false;
for (UINT t = s; t; t = (t - 1) & s) {
f(t, end);
if (end) break;
}
if constexpr (inc_empty) {
if (!end) f(UINT(0), end);
}
}
#line 6 "setfunc/boolean_range_add_point_get.hpp"
// O((4/3)^LOG) per query
template <typename Monoid>
struct Boolean_Range_Add_Point_Get {
using MX = Monoid;
using X = typename MX::value_type;
const int LOG;
vc<X> S;
array<u32, 3> mask;
/*
0: [x0,x1]
1: [x0+x1,x0]
2: [x0+x1,x1]
*/
Boolean_Range_Add_Point_Get(int LOG) : LOG(LOG), mask{} {
S.assign(1 << LOG, MX::id());
init_by_random();
}
void init_by_random() {
mask[0] = mask[1] = mask[2] = 0;
FOR(i, LOG) { mask[RNG(0, 3)] |= u32(1) << i; }
}
void init_by_query(const vc<pair<u32, u32>>& ADD, const vc<u32>& GET) {
for (auto& [lo, hi] : ADD) assert((lo & ~hi) == 0);
mask[0] = mask[1] = mask[2] = 0;
auto eval = [&]() -> ll {
ll ans = 0;
for (auto& [lo, hi] : ADD) {
u32 s = 0;
s ^= (lo ^ hi) & mask[0];
s ^= lo & mask[1];
s ^= (~hi) & mask[2];
ans += 1 << popcnt(s);
}
for (u32 i : GET) {
u32 s = 0;
s ^= (~i) & mask[1];
s ^= i & mask[2];
ans += 1 << popcnt(s);
}
return ans;
};
vc<int> I(LOG);
FOR(i, LOG) I[i] = i;
shuffle(I);
array<ll, 3> c;
for (int i : I) {
FOR(k, 3) {
mask[k] |= u32(1) << i, c[k] = eval(), mask[k] &= ~(u32(1) << i);
}
int k = min_element(all(c)) - c.begin();
mask[k] |= u32(1) << i;
}
}
void add(u32 lo, u32 hi, X x) {
assert((lo & ~hi) == 0);
u32 a = 0;
u32 s = 0;
u32 b = 0;
a ^= lo & mask[0];
s ^= (lo ^ hi) & mask[0];
a ^= (~hi) & mask[1];
s ^= lo & mask[1];
b ^= lo & mask[1];
a ^= lo & mask[2];
s ^= (~hi) & mask[2];
b ^= (~hi) & mask[2];
enumerate_all_subset<u32, true>(s, [&](u32 t) -> void {
X y = (__builtin_parity(t & b) ? MX::inverse(x) : x);
S[a | t] = MX::op(S[a | t], y);
});
}
X get(u32 i) {
u32 a = i & mask[0];
u32 s = 0;
s ^= (~i) & mask[1];
s ^= i & mask[2];
X ANS = MX::id();
enumerate_all_subset<u32, true>(
s, [&](u32 t) -> void { ANS = MX::op(ANS, S[a | t]); });
return ANS;
}
};