结论:
ACM比赛要bitset还是直接用STL
发现: 左移1e7次,STL的bitset要用0.7~0.8秒左右!
而一个long long / int 只要0.06秒左右
于是准备自己写一个bitset
然后……同样的操作,在inline后,0.48秒上下浮动。可以比STL快一倍,但是依然常数巨大。
只造了一小部分做实验,希望有菊苣能解决这个bitset低效率的问题,希望流传出高效率的bitset板子。
代码:只做了左移部分
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <tr1/unordered_map>
#include <ctime>
using namespace std::tr1;
using std::sort;
using std::bitset;
using std::max;
using std::cout;
using std::stack;
using std::cin;
using std::endl;
using std::swap;
using std::pair;
using std::vector;
using std::set;
using std::map;
using std::multiset;
using std::queue;
using std::greater;
using std::string;
using std::priority_queue;
using std::max_element;
using std::min_element;
using __gnu_pbds::pairing_heap_tag;
__gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> heap;
#define Hash unordered_map
#define pr(x) cout<<#x<<" = "<<x<<" "
#define prln(x) cout<<#x<<" = "<<x<<endl
#define LLL 64
typedef unsigned long long ULL;
int ref_size(int set_size)
{
return (int) floor(1.0 * set_size / LLL);
}
template<int set_size>
struct Bit_set
{
ULL a[(int) floor(1.0 * set_size / LLL)];//lll is the 'long long length'
Bit_set()
{
memset(a, 0, sizeof(a));
}
bool operator [] (int pos) const //作为右值
{
return a[pos / LLL] & (1 << (pos % LLL));
}
#define bit_first_idx(k) (k/LLL)
#define bit_second_idx(k) (k%LLL)
//第k位,首先位于first_idx的数字上,然后位于这个数字的second_idx位上
//下标从0开始
bool get_bit(int pos) const //得到第wz的01
{
return a[pos / LLL] & (1 << (pos % LLL) );
}
void set_bit(int pos, bool value) //第pos位的01是设置成
{
if (!value) a[pos / LLL] &= (1 << (pos % LLL)) ;
else a[pos / LLL] |= (1 << (pos % LLL)) ;
}
//Bit_set operator << ()
};
template<int set_size>
inline Bit_set<set_size> operator << (Bit_set<set_size> arg, int pos)
{
int tot_size = ref_size(set_size);
for (int i = tot_size; i ; -- i)
{
arg.a[i] <<= pos;
arg.a[i] |= (arg.a[i - 1] >> (LLL - pos) );
}
arg.a[0] <<= pos;
return arg;
}
Bit_set<64>q;
bitset<64>sb;
int main()
{
/*
q.a[0]=4;
cout<<q.a[0]<<endl;
for (int i = 0; i <= 10;++i)
q.set_bit(i, 1);
for (int i = 0; i <= 10; ++i)
cout << q.get_bit(i)<< " ";cout<<endl;
q = q << 4;
for (int i = 0; i <= 10; ++ i)
cout << q[i]<<" "; cout<<endl;
*/
double bg= clock();
//for (int i = 1;i<=1e7;++i)
// q<<1;
int p;
for (int i = 1; i<=1e7;++i)
q<<1;
cout<< (clock()-bg)/1000<<endl;
}