手写Bitset优化

一种优化方法,具体例子可以看这里

这里只是存一下手写Bitset的板子

struct Bitset
{
	unsigned a[1600];
	void reset()
	{
		memset(a,0,sizeof(a));
	}
	Bitset()
	{
		reset();
	}
	void flip(int x)
	{
		a[x>>5]^=1<<(x&31);
	}
	void set(int x)
	{
		a[x>>5]|=1<<(x&31);
	}
	void reset(int x)
	{
		a[x>>5]&=~(1<<(x&31));
	}
	int test(int x)
	{
		return (a[x>>5]>>(x&31))&1;
	}
	Bitset operator ~()const
	{
		Bitset ret;
		for(int i=0;i<1600;i++)ret.a[i]=~a[i];
		return ret;
	}
	Bitset operator &(const Bitset &b)const
	{
		Bitset ret;
		for(int i=0;i<1600;i++)ret.a[i]=a[i]&b.a[i];
		return ret;
	}
	Bitset operator |(const Bitset &b)const
	{
		Bitset ret;
		for(int i=0;i<1600;i++)ret.a[i]=a[i]|b.a[i];
		return ret;
	}
	Bitset operator ^(const Bitset &b)const
	{
		Bitset ret;
		for(int i=0;i<1600;i++)ret.a[i]=a[i]^b.a[i];
		return ret;
	}
	Bitset operator <<(const int t)const
	{
		Bitset ret;
		unsigned last=0;
		int high=t>>5,low=t&31;
		for(int i=0;i+high<1600;i++)
		{
			ret.a[i+high]=last|(a[i]<<low);
			if(low)last=a[i]>>(32-low);
		}
		return ret;
	}
	Bitset operator >>(const int t)const
	{
		Bitset ret;
		unsigned last=0;
		int high=t>>5,low=t&31;
		for(int i=1600-1;i>=high;i--)
		{
			ret.a[i-high]=last|(a[i]>>low);
			if(low)last=a[i]<<(32-low);
		}
		return ret;
	}
	vector<int> ones()const
	{
		vector<int> ret;
		for(int i=0;i<1600;i++)
		{
			unsigned tmp=a[i];
			while(tmp)
			{
				short t=__builtin_ctz(tmp);
				ret.pb((i<<5)|t);
				tmp^=1u<<t;
			}
		}
		return ret;
	}
}use,trans,cur;
原文地址:https://www.cnblogs.com/yijan/p/bitset.html