a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#include "../../../library/data-structure/dynamic-bitset.hpp"
#include "../../../library/fastio.hpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/10/ITP2_10_C"
int main() {
int q;
asalib::in >> q;
asalib::ds::DynamicBitset bs(64);
while (q--) {
int op;
asalib::in >> op;
if (op == 0) {
// test
int x;
asalib::in >> x;
asalib::out << (bs.test(x) ? 1 : 0) << '\n';
}
if (op == 1) {
// set
int x;
asalib::in >> x;
bs.set(x);
}
if (op == 2) {
// clear
int x;
asalib::in >> x;
bs.reset(x);
}
if (op == 3) {
// flip
int x;
asalib::in >> x;
bs.flip(x);
}
if (op == 4) {
// all
asalib::out << (bs.all() ? 1 : 0) << '\n';
}
if (op == 5) {
// any
asalib::out << (bs.any() ? 1 : 0) << '\n';
}
if (op == 6) {
// none
asalib::out << (bs.none() ? 1 : 0) << '\n';
}
if (op == 7) {
// count
asalib::out << bs.count() << '\n';
}
if (op == 8) {
// val
asalib::out << bs.to_ull() << '\n';
}
}
return 0;
}#line 1 "tests/data-structure/dynamic-bitset/itp2-10c.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#line 2 "library/data-structure/dynamic-bitset.hpp"
#line 4 "library/data-structure/dynamic-bitset.hpp"
#include <bit>
#line 6 "library/data-structure/dynamic-bitset.hpp"
#include <concepts>
#line 11 "library/data-structure/dynamic-bitset.hpp"
using namespace std;
namespace asalib::ds {
class DynamicBitset {
int siz = 0;
vector<uint64_t> bits;
public:
constexpr explicit DynamicBitset() = default;
constexpr DynamicBitset(const DynamicBitset&) = default;
constexpr ~DynamicBitset() = default;
constexpr DynamicBitset& operator=(const DynamicBitset&) = default;
constexpr explicit DynamicBitset(const int size) {
siz = size;
bits.reserve((size + 63) >> 6);
bits.resize((size + 63) >> 6, 0);
}
template<integral T>
constexpr explicit DynamicBitset(const int size, const T value) {
// TODO: ほかの型も考慮する
static_assert(sizeof(T) <= sizeof(uint64_t), "DynamicBitset: value type too large");
siz = size;
bits.reserve((size + 63) >> 6);
bits.resize((size + 63) >> 6, 0);
bits[0] = static_cast<uint64_t>(value);
trim();
}
constexpr DynamicBitset& operator&=(const DynamicBitset& other) {
assert(siz == other.siz);
const int sz = bits.size();
for (int i = 0; i < sz; ++i) bits[i] &= other.bits[i];
return *this;
}
constexpr DynamicBitset& operator|=(const DynamicBitset& other) {
assert(siz == other.siz);
const int sz = bits.size();
for (int i = 0; i < sz; ++i) bits[i] |= other.bits[i];
return *this;
}
constexpr DynamicBitset& operator^=(const DynamicBitset& other) {
assert(siz == other.siz);
const int sz = bits.size();
for (int i = 0; i < sz; ++i) bits[i] ^= other.bits[i];
return *this;
}
constexpr DynamicBitset& operator<<=(const int shift) {
if (shift == 0) return *this;
const int full = shift >> 6;
const int rem = shift & 63;
const int sz = bits.size();
if (full >= sz) {
ranges::fill(bits, 0);
return *this;
}
if (rem == 0) {
for (int i = sz - 1; i >= full; --i) bits[i] = bits[i - full];
ranges::fill(bits.begin(), bits.begin() + full, 0);
}
else {
for (int i = sz - 1; i >= full; --i) {
bits[i] = bits[i - full] << rem;
if (i - full - 1 >= 0) bits[i] |= bits[i - full - 1] >> (64 - rem);
}
ranges::fill(bits.begin(), bits.begin() + full, 0);
}
trim();
return *this;
}
constexpr DynamicBitset& operator>>=(const int shift) {
if (shift == 0) return *this;
const int full = shift >> 6;
const int rem = shift & 63;
const int sz = bits.size();
if (full >= sz) {
ranges::fill(bits, 0);
return *this;
}
if (rem == 0) {
for (int i = 0; i < sz - full; ++i) bits[i] = bits[i + full];
ranges::fill(bits.end() - full, bits.end(), 0);
}
else {
for (int i = 0; i < sz - full; ++i) {
bits[i] = bits[i + full] >> rem;
if (i + full + 1 < sz) bits[i] |= bits[i + full + 1] << (64 - rem);
}
ranges::fill(bits.end() - full, bits.end(), 0);
}
return *this;
}
constexpr DynamicBitset& set() {
for (int i = 0; i < ranges::ssize(bits); ++i) bits[i] = ~0ULL;
trim();
return *this;
}
constexpr DynamicBitset& set(const int pos, const bool val = true) {
if (pos >= siz) throw out_of_range("DynamicBitset::set: position out of range");
const int idx = pos >> 6;
const int bit = pos & 63;
if (val)
bits[idx] |= (1ULL << bit);
else
bits[idx] &= ~(1ULL << bit);
return *this;
}
constexpr DynamicBitset& reset() {
ranges::fill(bits, 0);
return *this;
}
constexpr DynamicBitset& reset(const int pos) { return set(pos, false); }
constexpr DynamicBitset operator~() const {
DynamicBitset res = *this;
res.flip();
return res;
}
constexpr DynamicBitset& flip() {
for (int i = 0; i < ranges::ssize(bits); ++i) bits[i] = ~bits[i];
trim();
return *this;
}
constexpr DynamicBitset& flip(const int pos) {
if (pos >= siz) throw out_of_range("DynamicBitset::flip: position out of range");
const int idx = pos >> 6;
const int bit = pos & 63;
bits[idx] ^= (1ULL << bit);
return *this;
}
constexpr int count() const {
int cnt = 0;
for (const uint64_t& b : bits) cnt += popcount(b);
return cnt;
}
constexpr int size() const { return siz; }
constexpr bool test(const int pos) const {
if (pos >= siz) throw out_of_range("DynamicBitset::test: position out of range");
const int idx = pos >> 6;
const int bit = pos & 63;
return (bits[idx] >> bit) & 1;
}
constexpr bool all() const {
for (const uint64_t& b : bits)
if (b != ~0ULL) return false;
return true;
}
constexpr bool any() const {
for (const uint64_t& b : bits)
if (b != 0) return true;
return false;
}
constexpr bool none() const {
for (const uint64_t& b : bits)
if (b != 0) return false;
return true;
}
constexpr uint64_t to_ull() const {
if (bits.size() > 1) throw logic_error("DynamicBitset::to_ull: overflow");
if (bits.empty()) return 0;
return bits[0];
}
constexpr string to_string() const {
string res;
res.reserve(siz);
for (int i = 0; i < siz; ++i) res += (test(i) ? '1' : '0');
ranges::reverse(res);
return res;
}
constexpr bool operator==(const DynamicBitset& other) const {
if (siz != other.siz) return false;
for (int i = 0; i < ranges::ssize(bits); ++i)
if (bits[i] != other.bits[i]) return false;
return true;
}
constexpr bool operator!=(const DynamicBitset& other) const {
return !(*this == other);
}
constexpr DynamicBitset operator<<(const uint64_t shift) const {
DynamicBitset res = *this;
res <<= shift;
return res;
}
constexpr DynamicBitset operator>>(const uint64_t shift) const {
DynamicBitset res = *this;
res >>= shift;
return res;
}
constexpr DynamicBitset operator&(const DynamicBitset& other) const {
DynamicBitset res = *this;
res &= other;
return res;
}
constexpr DynamicBitset operator|(const DynamicBitset& other) const {
DynamicBitset res = *this;
res |= other;
return res;
}
constexpr DynamicBitset operator^(const DynamicBitset& other) const {
DynamicBitset res = *this;
res ^= other;
return res;
}
private:
constexpr void trim() {
if (const int rem = siz & 63; rem != 0 && !bits.empty()) bits.back() &= (1ULL << rem) - 1;
}
};
} // namespace asalib::ds
#line 2 "library/fastio.hpp"
#line 11 "library/fastio.hpp"
#include <type_traits>
using namespace std;
// TODO: 何も入力がない場合にも対応する?
namespace asalib {
namespace _internal {
class FastIn {
static constexpr unsigned int BUFFER_SIZE = 1 << 20;
static constexpr unsigned int MAX_TOKEN_SIZE = 64;
array<char, BUFFER_SIZE> read_buffer = {};
array<char, BUFFER_SIZE>::iterator read_ptr = nullptr;
unsigned int read_size = 0;
public:
FastIn() {
read_buffer.fill(0);
read_ptr = read_buffer.begin();
load();
}
private:
void load() {
// まだ読んでないデータを前に持ってくる
memcpy(read_buffer.begin(), read_ptr, read_size - (read_ptr - read_buffer.begin()));
read_size -= read_ptr - read_buffer.begin();
read_ptr = read_buffer.begin();
// stdin から読み込み
read_size += fread(read_buffer.begin() + read_size, 1, BUFFER_SIZE - read_size, stdin);
}
void skip_space() {
// 制御文字 + space
// DEL (127 = 0x7F) がコーナーケースだがまあ使わんやろ
while (*read_ptr <= ' ') ++read_ptr;
}
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
void _read_uint(T& x) {
x = 0;
while (true) {
uint64_t v;
memcpy(&v, read_ptr, 8);
// '0' -> 0 処理
// こっちを先にやることで制御文字系もはじく
v -= 0x30'30'30'30'30'30'30'30;
// ASCII 範囲外
if (v & 0x80'80'80'80'80'80'80'80) break;
// 桁ごとの数字から数値に
v = (v * 10 + (v >> 8)) & 0x00ff00ff00ff00ff;
v = (v * 100 + (v >> 16)) & 0x0000ffff0000ffff;
v = (v * 10000 + (v >> 32)) & 0x00000000ffffffff;
x = 1'0000'0000 * x + v;
read_ptr += 8;
}
while (true) {
uint32_t v;
memcpy(&v, read_ptr, 4);
v -= 0x30'30'30'30;
if (v & 0x80'80'80'80) break;
v = (v * 10 + (v >> 8)) & 0x00ff00ff;
v = (v * 100 + (v >> 16)) & 0x0000ffff;
x = 1'0000 * x + v;
read_ptr += 4;
}
while (true) {
uint16_t v;
memcpy(&v, read_ptr, 2);
v -= 0x3030;
if (v & 0x8080) break;
v = (v * 10 + (v >> 8)) & 0x00ff;
x = 100 * x + v;
read_ptr += 2;
}
if (*read_ptr > ' ') x = 10 * x + (*read_ptr++ & 0x0f);
}
void read_commonop() {
// そろそろ限界なら読み込み
if ((read_ptr - read_buffer.begin()) + MAX_TOKEN_SIZE >= BUFFER_SIZE) load();
skip_space();
}
public:
FastIn& operator>>(char& x) {
read_commonop();
x = *read_ptr++;
return *this;
}
FastIn& operator>>(string& x) {
read_commonop();
x.clear();
while (*read_ptr > ' ') {
x += *read_ptr++;
if (read_ptr == read_buffer.end()) load();
}
return *this;
}
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
FastIn& operator>>(T& x) {
read_commonop();
if constexpr (is_signed_v<T> || is_same_v<T, __int128_t>) {
if (*read_ptr == '-') {
++read_ptr;
_read_uint(x);
x = -x;
return *this;
}
}
_read_uint(x);
return *this;
}
};
class FastOut {
static constexpr unsigned int BUFFER_SIZE = 1 << 20;
static constexpr unsigned int MAX_TOKEN_SIZE = 64;
static constexpr unsigned int MAX_PRECOMPUTE_NUM = 1'0000;
static constexpr unsigned int MAX_PRECOMPUTE_NUM_DIGIT = 4;
array<char, BUFFER_SIZE> write_buffer = {};
array<char, BUFFER_SIZE>::iterator write_ptr = nullptr;
public:
FastOut() {
write_buffer.fill(0);
write_ptr = write_buffer.begin();
}
~FastOut() { flush(); }
private:
static constexpr array<char, MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT> digits = [] {
array<char, MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT> digits {};
for (unsigned int i = 0; i < MAX_PRECOMPUTE_NUM; ++i) {
unsigned int x = i;
for (int j = MAX_PRECOMPUTE_NUM_DIGIT - 1; j >= 0; --j) {
digits[i * MAX_PRECOMPUTE_NUM_DIGIT + j] = '0' + (x % 10);
x /= 10;
}
}
return digits;
}();
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
static constexpr array<T, numeric_limits<T>::digits10 + 1> Pow10 = [] {
array<T, numeric_limits<T>::digits10 + 1> pow10 {};
pow10[0] = 1;
for (unsigned int i = 1; i < pow10.size(); ++i) pow10[i] = pow10[i - 1] * 10;
return pow10;
}();
void write_commonop() {
// そろそろ限界なら書き込む
if ((write_ptr - write_buffer.begin()) + MAX_TOKEN_SIZE >= BUFFER_SIZE) flush();
}
void putchar(const char& x) { *write_ptr++ = x; }
template<typename T, int NumDig>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
void _write_uint_top(const T& x) {
// leading-zero を書き込まないようにする
if constexpr (NumDig > 1) {
if (x < Pow10<T>[NumDig - 1]) {
_write_uint_top<T, NumDig - 1>(x);
return;
}
}
copy_n(digits.begin() + (x + 1) * MAX_PRECOMPUTE_NUM_DIGIT - NumDig, NumDig, write_ptr);
write_ptr += NumDig;
}
template<typename T, int NumDig>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
void _write_uint(const T& x) {
if constexpr (NumDig >= 0) {
if constexpr (NumDig > MAX_PRECOMPUTE_NUM_DIGIT) _write_uint<T, NumDig - MAX_PRECOMPUTE_NUM_DIGIT>(x / MAX_PRECOMPUTE_NUM);
copy_n(digits.begin() + x % MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT, MAX_PRECOMPUTE_NUM_DIGIT, write_ptr);
write_ptr += MAX_PRECOMPUTE_NUM_DIGIT;
}
}
template<typename T, int NumDig>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
void _write_uint_root(const T& x) {
if constexpr (is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) {
// 128bit 除算はおそいので 64bit 除算を使う
if (x < Pow10<T>[16]) {
_write_uint_root<uint64_t, NumDig>(x);
}
else if (x < Pow10<T>[32]) {
_write_uint_root<uint64_t, NumDig>(x / Pow10<T>[16]);
_write_uint<uint64_t, 16>(x % Pow10<T>[16]);
}
else {
_write_uint_root<uint64_t, NumDig>(x / Pow10<T>[32]);
_write_uint<uint64_t, 16>(x % Pow10<T>[32] / Pow10<T>[16]);
_write_uint<uint64_t, 16>(x % Pow10<T>[16]);
}
return;
}
if constexpr (NumDig < numeric_limits<T>::digits10) {
if (x >= Pow10<T>[NumDig]) {
_write_uint_root<T, NumDig + MAX_PRECOMPUTE_NUM_DIGIT>(x);
return;
}
}
_write_uint_top<T, MAX_PRECOMPUTE_NUM_DIGIT>(x / Pow10<T>[NumDig - MAX_PRECOMPUTE_NUM_DIGIT]);
if constexpr (NumDig > MAX_PRECOMPUTE_NUM_DIGIT) _write_uint<T, NumDig - MAX_PRECOMPUTE_NUM_DIGIT>(x % Pow10<T>[NumDig - MAX_PRECOMPUTE_NUM_DIGIT]);
}
public:
void flush() {
fwrite(write_buffer.begin(), 1, write_ptr - write_buffer.begin(), stdout);
write_ptr = write_buffer.begin();
}
FastOut& operator<<(const char& x) {
write_commonop();
putchar(x);
return *this;
}
FastOut& operator<<(const string& x) {
write_commonop();
unsigned int idx = 0;
while (idx < x.size()) {
const unsigned int siz = min(BUFFER_SIZE - static_cast<unsigned int>(write_ptr - write_buffer.begin()), static_cast<unsigned int>(x.size()) - idx);
copy_n(x.begin() + idx, siz, write_ptr);
write_ptr += siz;
idx += siz;
if (write_ptr == write_buffer.end()) flush();
}
return *this;
}
template<typename T>
requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
FastOut& operator<<(const T& x) {
write_commonop();
if constexpr (is_signed_v<T> || is_same_v<T, __int128_t>) {
if (x < 0) {
putchar('-');
_write_uint_root<T, MAX_PRECOMPUTE_NUM_DIGIT>(-x);
return *this;
}
}
_write_uint_root<T, MAX_PRECOMPUTE_NUM_DIGIT>(x);
return *this;
}
};
} // namespace _internal
inline _internal::FastIn in;
inline _internal::FastOut out;
} // namespace asalib
#line 9 "tests/data-structure/dynamic-bitset/itp2-10c.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/10/ITP2_10_C"
int main() {
int q;
asalib::in >> q;
asalib::ds::DynamicBitset bs(64);
while (q--) {
int op;
asalib::in >> op;
if (op == 0) {
// test
int x;
asalib::in >> x;
asalib::out << (bs.test(x) ? 1 : 0) << '\n';
}
if (op == 1) {
// set
int x;
asalib::in >> x;
bs.set(x);
}
if (op == 2) {
// clear
int x;
asalib::in >> x;
bs.reset(x);
}
if (op == 3) {
// flip
int x;
asalib::in >> x;
bs.flip(x);
}
if (op == 4) {
// all
asalib::out << (bs.all() ? 1 : 0) << '\n';
}
if (op == 5) {
// any
asalib::out << (bs.any() ? 1 : 0) << '\n';
}
if (op == 6) {
// none
asalib::out << (bs.none() ? 1 : 0) << '\n';
}
if (op == 7) {
// count
asalib::out << bs.count() << '\n';
}
if (op == 8) {
// val
asalib::out << bs.to_ull() << '\n';
}
}
return 0;
}