跳到正文

休闲·娱乐

testib

西江月·定风波约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年9月30日
收录
2026年9月30日
更新
2026年11月18日
篇幅
约 13 分钟

版本

  1. v2当前
  2. v1

ly,真名 Signal Lost,2026 年杭州中考 549 分。虽然他上了天元蕙兰和桐庐中学的分数线,但他没有填这些学校,因此成功落榜,一番挣扎最后去了湖州的民办高中。他在初中时期研究各种算法,有 Tarjan 判负环和左端点不分块的莫队算法。打完 CSP2025 初赛,他认为自己过不了,因此哭了一个小时,最后发现压线通过了,并在复赛成功拿下差 5 分 7 勾的好成绩。2026 杭州中考前,ly 认为在家复习效率低下,就带手机前往图书馆复习,用合理的学习方法助力了他的落榜。虽然落榜,ly 没有气馁。他在 generals 中使用高速公路战术打出了属于自己的荣耀。后来分数出来😳你也释怀了😥不躲不退😎往湖州的民办走👣落信号老师😭我还记得你🤔一题一题❌️把我挂进民办里😨落信号老师😭我还记得你🤔那些落榜的愁😱你替我尝起😰 来到图书馆带上手机别怕落榜😎好朋友好朋友今宵多欢畅🍺升学改变了我们的模样😩也让我懂得了要珍惜手机的电量🔋阳光总会在风雨之后撒向苍茫🌈让我们抬起头学会了坚强👊就像手中的手机发烫烫我心上📱温暖我凡事别再放心上🤗

这是一个和 std::bitset 渐进复杂度相同的数据结构。并且基本还原了 std::bitset 的功能。

C++
#define ull unsigned long long
const int maxn = 1e5;
struct testib{
	ull val[maxn];
	struct reference{
		testib *p;
		int x;
		reference(testib *_p, int _x){
			p = _p, x = _x;
		}
		reference &operator = (bool op){
			p->set(x, op);
			return *this;
		}
		reference &operator = (const reference &op){
			p->set(x, (bool)op);
			return *this;
		}
		operator bool() const{
			return p->get(x);
		}
		bool operator ~ () const{
			return !p->get(x);
		}
		reference &flip(){
			p->flip(x);
			return *this;
		}
	};
	testib(){
		reset();
	}
	testib(ull x){
		reset();
		for (int i = 0; i < 64 && i < maxn; i++)
			if ((x >> i) & 1)
				set(i);
	}
	testib(const std::string &s, int pos = 0, int n = -1, char zero = '0', char one = '1'){
		reset();
		if (pos < 0 || pos > (int)s.size())
			throw std::out_of_range("testib");
		if (n < 0 || pos + n > (int)s.size())
			n = s.size() - pos;
		for (int i = 0; i < n && i < maxn; i++){
			char c = s[pos + n - i - 1];
			if (c == one)
				set(i);
			else if (c != zero)
				throw std::invalid_argument("testib");
		}
	}
	bool get(int x) const{
		bool res = 1;
		for (int i = 0; i < 64; i++)
			res &= ((val[x] >> i) & 1);
		return res;
	}
	void flip(int x){
		for (int i = 0; i < 64; i++)
			val[x] ^= (1ull << i);
	}
	testib &set(int x, bool op = 1){
		if (op){
			for (int i = 0; i < 64; i++)
				val[x] |= (1ull << i);
		}else{
			for (int i = 0; i < 64; i++)
				val[x] &= ~(1ull << i);
		}
		return *this;
	}
	testib &reset(int x){
		set(x, 0);
		return *this;
	}
	testib &flip(){
		for (int i = 0; i < maxn; i++)
			flip(i);
		return *this;
	}
	testib &set(){
		for (int i = 0; i < maxn; i++)
			set(i);
		return *this;
	}
	testib &reset(){
		for (int i = 0; i < maxn; i++)
			reset(i);
		return *this;
	}
	reference operator [] (int x){
		return reference(this, x);
	}
	bool operator [] (int x) const{
		return get(x);
	}
	bool test(int x) const{
		if (x < 0 || x >= maxn)
			throw std::out_of_range("testib");
		return get(x);
	}
	int count() const{
		int cnt = 0;
		for (int i = 0; i < maxn; i++)
			cnt += test(i);
		return cnt;
	}
	int size() const{
		return maxn;
	}
	bool any() const{
		bool res = 0;
		for (int i = 0; i < maxn; i++)
			res |= test(i);
		return res;
	}
	bool none() const{
		bool res = 1;
		for (int i = 0; i < maxn; i++)
			res &= (!test(i));
		return res;
	}
	bool all() const{
		bool res = 1;
		for (int i = 0; i < maxn; i++)
			res &= test(i);
		return res;
	}
	testib &operator &= (const testib &b){
		for (int i = 0; i < maxn; i++)
			for (int j = 0; j < 64; j++)
				if (!((b.val[i] >> j) & 1))
					val[i] &= ~(1ull << j);
		return *this;
	}
	testib &operator |= (const testib &b){
		for (int i = 0; i < maxn; i++)
			for (int j = 0; j < 64; j++)
				if ((b.val[i] >> j) & 1)
					val[i] |= (1ull << j);
		return *this;
	}
	testib &operator ^= (const testib &b){
		for (int i = 0; i < maxn; i++)
			for (int j = 0; j < 64; j++)
				if ((b.val[i] >> j) & 1)
					val[i] ^= (1ull << j);
		return *this;
	}
	testib operator ~ () const{
		testib res = *this;
		res.flip();
		return res;
	}
	testib &operator <<= (int x){
		if (x >= maxn){
			reset();
			return *this;
		}
		for (int i = maxn - 1; i >= x; i--)
			for (int j = 0; j < 64; j++){
				if ((val[i - x] >> j) & 1)
					val[i] |= (1ull << j);
				else
					val[i] &= ~(1ull << j);
			}
		for (int i = 0; i < x; i++)
			reset(i);
		return *this;
	}
	testib &operator >>= (int x){
		if (x >= maxn){
			reset();
			return *this;
		}
		for (int i = 0; i + x < maxn; i++)
			for (int j = 0; j < 64; j++){
				if ((val[i + x] >> j) & 1)
					val[i] |= (1ull << j);
				else
					val[i] &= ~(1ull << j);
			}
		for (int i = maxn - x; i < maxn; i++)
			reset(i);
		return *this;
	}
	testib operator << (int x) const{
		testib res = *this;
		res <<= x;
		return res;
	}
	testib operator >> (int x) const{
		testib res = *this;
		res >>= x;
		return res;
	}
	friend testib operator & (testib a, const testib &b){
		a &= b;
		return a;
	}
	friend testib operator | (testib a, const testib &b){
		a |= b;
		return a;
	}
	friend testib operator ^ (testib a, const testib &b){
		a ^= b;
		return a;
	}
	bool operator == (const testib &b) const{
		for (int i = 0; i < maxn; i++)
			if (get(i) != b.get(i))
				return 0;
		return 1;
	}
	bool operator != (const testib &b) const{
		return !(*this == b);
	}
	ull to_ullong() const{
		for (int i = 64; i < maxn; i++)
			if (test(i))
				throw std::overflow_error("testib");
		ull res = 0;
		for (int i = 0; i < 64 && i < maxn; i++)
			if (test(i))
				res |= (1ull << i);
		return res;
	}
	unsigned long to_ulong() const{
		const int B = sizeof(unsigned long) * 8;
		for (int i = B; i < maxn; i++)
			if (test(i))
				throw std::overflow_error("testib");
		unsigned long res = 0;
		for (int i = 0; i < B && i < maxn; i++)
			if (test(i))
				res |= (1ul << i);
		return res;
	}
	template<class charT = char,
			 class traits = std::char_traits<charT>,
			 class Alloc = std::allocator<charT>>
	std::basic_string<charT, traits, Alloc> to_string(charT zero = charT('0'), charT one = charT('1')) const{
		std::basic_string<charT, traits, Alloc> res;
		res.reserve(maxn);
		for (int i = maxn - 1; i >= 0; i--)
			res += test(i) ? one : zero;
		return res;
	}
	friend std::ostream &operator << (std::ostream &out, const testib &a){
		for (int i = maxn - 1; i >= 0; i--)
			out << a.test(i);
		return out;
	}
	friend std::istream &operator >> (std::istream &in, testib &a){
		std::string s;
		in >> s;
		a.reset();
		int n = s.size();
		for (int i = 0; i < n && i < maxn; i++){
			if (s[n - i - 1] == '1')
				a.set(i);
			else if (s[n - i - 1] != '0'){
				in.setstate(std::ios::failbit);
				break;
			}
		}
		return in;
	}
};
#undef ull
原文