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;
    
  • 相关阅读:
    Linux 查看CPU信息,机器型号,内存等信息
    TCPdump抓包命令详解
    nginx https 转发
    滚动效果
    phpexcel中文手册(转)
    Java数组操作十大方法 (转)
    ajax防止重复提交
    信用评分卡(A卡/B卡/C卡)的模型简介及开发流程|干货
    求方差分析与两样本T检验 区别
    互联网运营中的10大数据分析方法
  • 原文地址:https://www.cnblogs.com/yijan/p/bitset.html
Copyright © 2011-2022 走看看