手写 bitset
压行
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Size=(1<<20)+1;
char buf[Size],*p1=buf,*p2=buf;
char buffer[Size];
int op1=-1;
const int op2=Size-1;
#define getchar() \
(tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt) \
? EOF \
: *ss++)
char In[1<<20],*ss=In,*tt=In;
inline int read()
{
int x=0,c=getchar(),f=0;
for(;c>'9'||c<'0';f=c=='-',c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
inline void write(int x)
{
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
#ifndef ONLINE_JUDGE
#define ONLINE_JUDGE
#endif
struct Bit{
// 手写 bitset
#ifdef int
#define LONG_LONG_EXISTS
#undef int
#endif
#define int unsigned long long
static constexpr int N=4e4+64;
static constexpr int M=N/64+2,base=64;
int bit[M];
void reset() { memset(bit,0,sizeof(bit)); }
void clear() { memset(bit,0,sizeof(bit)); }
void set_1() { memset(bit,255,sizeof(bit)); }
void set(bool x) { x?set_1():reset(); }
void set(int pos,int v) { if(v) bit[pos>>6]|=( (int)1ULL<<(pos&63) ); else bit[pos>>6]&=~((int)1ULL<<(pos&63)); }
void operator |= (const Bit &a) { for(int i=0;i<M;i++) bit[i]|=a.bit[i]; }
void operator &= (const Bit &a) { for(int i=0;i<M;i++) bit[i]&=a.bit[i]; }
void operator ^= (const Bit &a) { for(int i=0;i<M;i++) bit[i]^=a.bit[i]; }
Bit operator | (const Bit &a) { Bit res; res.reset(); for(int i=0;i<M;i++) res.bit[i]=bit[i]|a.bit[i]; return res;}
Bit operator & (const Bit &a) { Bit res; res.reset(); for(int i=0;i<M;i++) res.bit[i]=bit[i]&a.bit[i]; return res;}
Bit operator ^ (const Bit &a) { Bit res; res.reset(); for(int i=0;i<M;i++) res.bit[i]=bit[i]^a.bit[i]; return res;}
// bool operator [](int x) const { return (bit[x>>6]>>(x&63))&1; }
struct Reference{ Bit *b; int pos; Reference(Bit *bb, int p): b(bb), pos(p) {} Reference &operator=(int v) { if(v) b->bit[pos>>6] |= ((int)1ULL << (pos&63)); else b->bit[pos>>6] &= ~((int)1ULL << (pos&63)); return *this; } operator int() const { return (b->bit[pos>>6] >> (pos&63)) & 1; } operator bool() const { return operator int(); } };
Reference operator [](int x) { return Reference(this, x); }
int count() { int res=0; for(int i=0;i<M;i++) res+=__builtin_popcountll(bit[i]); return res; }
int kth(int k) { int cnt=0; for(int i=0;i<M;i++) { cnt+=__builtin_popcountll(bit[i]); if(cnt>=k) { cnt-=__builtin_popcountll(bit[i]); int nw=0; for(int j=0;j<base;j++) { cnt+=(bit[i]>>j)&1; if(cnt==k) return (i<<6)+j; } } } return -1; }
int _Find_next(int pos) { pos++; if((bit[pos>>6]>>(pos&63))) return pos+__builtin_ctzll((bit[pos>>6]>>(pos&63))); for(int i=(pos>>6)+1;i<M;i++) if(bit[i]) return (i<<6)+__builtin_ctzll(bit[i]); return -1; }
int _Find_first() { return (bit[0]&1)?0:_Find_next(0); }
#undef int
#ifdef LONG_LONG_EXISTS
#define int long long
#endif
};
signed main()
{
// #ifndef ONLINE_JUDGE
// freopen("a.in","r",stdin);
freopen("bitset.out","w",stdout);
// #endif
Bit b;
b.clear();
cout<<bool(b[0])<<"\n";
//mt19937_64 myrand(time(0));
return 0;
}
不压行
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Size=(1<<20)+1;
char buf[Size],*p1=buf,*p2=buf;
char buffer[Size];
int op1=-1;
const int op2=Size-1;
#define getchar() \
(tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt) \
? EOF \
: *ss++)
char In[1<<20],*ss=In,*tt=In;
inline int read()
{
int x=0,c=getchar(),f=0;
for(;c>'9'||c<'0';f=c=='-',c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
inline void write(int x)
{
if(x<0) x=-x,putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
#ifndef ONLINE_JUDGE
#define ONLINE_JUDGE
#endif
struct Bit{
// 手写 b
#ifdef int
#define LONG_LONG_EXISTS
#undef int
#endif
#define int unsigned long long
static constexpr int N=4e4+64;
static constexpr int M=N/64+2,base=64;
int bit[M];
void reset() { memset(bit,0,sizeof(bit)); }
void clear() { memset(bit,0,sizeof(bit)); }
void set_1() { memset(bit,255,sizeof(bit)); }
void set(bool x) { x?set_1():reset(); }
void set(int pos,int v) { if(v) bit[pos>>6]|=( (int)1ULL<<(pos&63) ); else bit[pos>>6]&=~((int)1ULL<<(pos&63)); }
void operator |= (const Bit &a) { for(int i=0;i<M;i++) bit[i]|=a.bit[i]; }
void operator &= (const Bit &a) { for(int i=0;i<M;i++) bit[i]&=a.bit[i]; }
void operator ^= (const Bit &a) { for(int i=0;i<M;i++) bit[i]^=a.bit[i]; }
Bit operator | (const Bit &a) { Bit res; res.reset(); for(int i=0;i<M;i++) res.bit[i]=bit[i]|a.bit[i]; return res;}
Bit operator & (const Bit &a) { Bit res; res.reset(); for(int i=0;i<M;i++) res.bit[i]=bit[i]&a.bit[i]; return res;}
Bit operator ^ (const Bit &a) { Bit res; res.reset(); for(int i=0;i<M;i++) res.bit[i]=bit[i]^a.bit[i]; return res;}
// bool operator [](int x) const { return (bit[x>>6]>>(x&63))&1; }
struct Reference{
Bit *b;
int pos;
Reference(Bit *bb, int p): b(bb), pos(p) {}
Reference &operator=(int v) { if(v) b->bit[pos>>6] |= ((int)1ULL << (pos&63)); else b->bit[pos>>6] &= ~((int)1ULL << (pos&63)); return *this; }
operator int() const { return (b->bit[pos>>6] >> (pos&63)) & 1; }
operator bool() const { return operator int(); }
};
Reference operator [](int x) { return Reference(this, x); }
int count() { int res=0; for(int i=0;i<M;i++) res+=__builtin_popcountll(bit[i]); return res; }
int kth(int k)
{
int cnt=0;
for(int i=0;i<M;i++)
{
cnt+=__builtin_popcountll(bit[i]);
if(cnt>=k)
{
cnt-=__builtin_popcountll(bit[i]);
int nw=0;
for(int j=0;j<base;j++)
{
cnt+=(bit[i]>>j)&1;
if(cnt==k) return (i<<6)+j;
}
}
}
return -1;
}
int _Find_next(int pos)
{
pos++;
if((bit[pos>>6]>>(pos&63))) return pos+__builtin_ctzll((bit[pos>>6]>>(pos&63)));
for(int i=(pos>>6)+1;i<M;i++) if(bit[i]) return (i<<6)+__builtin_ctzll(bit[i]);
return -1;
}
int _Find_first() { return (bit[0]&1)?0:_Find_next(0); }
#undef int
#ifdef LONG_LONG_EXISTS
#define int long long
#endif
};
signed main()
{
// #ifndef ONLINE_JUDGE
// freopen("a.in","r",stdin);
freopen("bitset.out","w",stdout);
// #endif
Bit b;
b.clear();
cout<<bool(b[0])<<"\n";
//mt19937_64 myrand(time(0));
return 0;
}
以下是博客签名,正文无关
本文来自博客园,作者:Wy_x,转载请在文首注明原文链接:https://www.cnblogs.com/Wy-x/p/19249619
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议(CC-BY-NC-SA 4.0 协议)进行许可。

浙公网安备 33010602011771号