Range Xor Range Xor
(acted_monoid/range_xor_range_xor.hpp)
- View this file on GitHub
- Last update: 2026-07-21 20:17:47+09:00
- Include:
#include "acted_monoid/range_xor_range_xor.hpp"
Overview
An acted monoid that supports range bitwise XOR operations and range XOR sum queries.
Important Usage Note
When a value $f$ is XORed to all elements in a segment, the total XOR sum of the segment changes by $f$ only if the segment’s length is odd. Therefore, value_type holds the size of the segment. Use the make(val) helper function to initialize leaf nodes.
Example
#include "ds/segtree/lazy_segtree.hpp"
#include "acted_monoid/range_xor_range_xor.hpp"
#include <iostream>
#include <vector>
using AM = m1une::acted_monoid::RangeXorRangeXor<long long>;
int main() {
std::vector<long long> A = {1, 2, 3, 4, 5};
int N = A.size();
std::vector<AM::value_type> init_nodes(N);
for (int i = 0; i < N; ++i) {
init_nodes[i] = AM::make(A[i]);
}
m1une::ds::LazySegtree<AM> seg(init_nodes);
seg.apply(1, 4, 7);
std::cout << seg.prod(1, 4).val << "\n";
return 0;
}
Interface and Complexity
This is a stateless acted-monoid tag. Lazy data structures use its public
value_type, operator_type, id(), op(a, b), op_id(), op_comp(f, g),
and mapping(f, x) members. Helpers such as make(...), shifted mappings, or
reversal-aware mappings are described above when the header provides them.
The static operations are $O(1)$ for the scalar metadata stored by these range acted monoids, aside from the cost of the underlying arithmetic type.
Verified with
Code
#ifndef M1UNE_ACTED_MONOID_RANGE_XOR_RANGE_XOR_HPP
#define M1UNE_ACTED_MONOID_RANGE_XOR_RANGE_XOR_HPP 1
namespace m1une {
namespace acted_monoid {
template <typename T>
struct RangeXorRangeXorNode {
T val;
long long size;
};
template <typename T>
struct RangeXorRangeXor {
using value_type = RangeXorRangeXorNode<T>;
using operator_type = T;
static constexpr bool commutative = true;
static constexpr bool operator_commutative = true;
static constexpr value_type id() {
return {T(0), 0};
}
static constexpr value_type op(const value_type& a, const value_type& b) {
return {a.val ^ b.val, a.size + b.size};
}
static constexpr value_type inv(const value_type& x) {
return {x.val, -x.size};
}
static constexpr operator_type op_id() {
return T(0);
}
static constexpr operator_type op_comp(const operator_type& f, const operator_type& g) {
return f ^ g;
}
static constexpr value_type mapping(const operator_type& f, const value_type& x) {
if (x.size % 2 != 0) {
return {x.val ^ f, x.size};
}
return x;
}
static constexpr value_type make(const T& val) {
return {val, 1};
}
};
} // namespace acted_monoid
} // namespace m1une
#endif // M1UNE_ACTED_MONOID_RANGE_XOR_RANGE_XOR_HPP#line 1 "acted_monoid/range_xor_range_xor.hpp"
namespace m1une {
namespace acted_monoid {
template <typename T>
struct RangeXorRangeXorNode {
T val;
long long size;
};
template <typename T>
struct RangeXorRangeXor {
using value_type = RangeXorRangeXorNode<T>;
using operator_type = T;
static constexpr bool commutative = true;
static constexpr bool operator_commutative = true;
static constexpr value_type id() {
return {T(0), 0};
}
static constexpr value_type op(const value_type& a, const value_type& b) {
return {a.val ^ b.val, a.size + b.size};
}
static constexpr value_type inv(const value_type& x) {
return {x.val, -x.size};
}
static constexpr operator_type op_id() {
return T(0);
}
static constexpr operator_type op_comp(const operator_type& f, const operator_type& g) {
return f ^ g;
}
static constexpr value_type mapping(const operator_type& f, const value_type& x) {
if (x.size % 2 != 0) {
return {x.val ^ f, x.size};
}
return x;
}
static constexpr value_type make(const T& val) {
return {val, 1};
}
};
} // namespace acted_monoid
} // namespace m1une