zoukankan      html  css  js  c++  java
  • 手写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;
    
  • 相关阅读:
    元素定位8种方法
    excel做数据驱动
    selenium colse()与quit()方法的区别
    flask 获取request参数的几种形式
    hdu1272
    土地征用
    任务安排
    征途
    锯木厂选址
    特别行动队
  • 原文地址:https://www.cnblogs.com/yijan/p/bitset.html
Copyright © 2011-2022 走看看