清新小容器 — bitset
Nickel_Angel · · 个人记录
bitset 是 STL 中一个不错的容器,在一些可以使用状压的题目中,她可以为记录二进制状态提供更大的空间。
bitset 位运算的时间复杂度为
P1537 弹珠
这是一个布尔背包问题,若设所有弹珠的价值和为 bitor
但我们发现在本题中我们只关心某个价值能不能被表示出来,而不关心它的代价或方案数……故考虑使用 bitset 优化。我们可以直接将状态记录在一个 01 串里,其中若该 01 串的第
Code:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
namespace Primary
{
int w[10], sum;
bitset<10010> help;
void main()
{
int Case = 0;
while (++Case)
{
help.reset(), sum = 0;
for (int i = 1; i < 7; ++i)
{
scanf("%d", w + i);
sum += w[i] * i;
}
if (!sum) return;
printf("Collection #%d:\n", Case);
if (sum & 1)
{
puts("Can't be divided.");
puts("");
continue;
}
help.set(0);
for (int i = 1; i < 7; ++i)
{
for (int j = 1; j <= w[i]; ++j)
{
help |= (help << i);
}
}
if (help.test(sum >> 1)) puts("Can be divided.");
else puts("Can't be divided.");
puts("");
}
}
}
int main()
{
Primary::main();
return 0;
}
P2757 等差子序列
题目大意为给定一个全排列,询问在其中是否存在一个长度至少为
本题正解为线段树 + hash,但我们可以使用自带小常数的 bitset 通过本题。
不难发现,由于本题给定的
我们每一次更新(假定我们当前扫到序列中的值为
考虑到我们可以使用 bitset 代替布尔数组,而为了便于我们判断回文串,我们可以考虑同时维护两个 bitset:
一个 bitset (
一个 bitset (
在依次将序列中的数(设其为
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
namespace Primary
{
const int maxn = 10010;
int n, T, x;
bitset<maxn> pre, suf, base, cmp1, cmp2;
bool check(int x)
{
//下面的式子推了半天QAQ但奈何不知如何表达
if (x > n / 2)//这里对称区域的边界为 1
{
cmp1 = (pre >> (x - 1));
cmp2 = (suf >> (n - x)) & (base >> (x - 1));
}
else//这里对称区域的边界为 n
{
cmp1 = (pre & (base >> (n - x)));
cmp2 = ((suf >> (n - 2 * x + 1)) & (base >> (n - x)));
}
cmp1.set(0), cmp2.set(0);//由于 bitset 是从第一位开始使用的,故要消除第 0 位的影响
return cmp1 != cmp2;
}
void main()
{
scanf("%d", &T);
bool flag;
while (T--)
{
pre.reset(), suf.reset(), base.reset();//多组数据,注意清空
scanf("%d", &n);
for (int i = 1; i <= n; ++i) base.set(i);
flag = false;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &x);
if (flag) continue;
pre.set(x), suf.set(n - x + 1);
if (check(x)) flag = true;
}
puts(flag ? "Y" : "N");
}
}
}
int main()
{
Primary::main();
return 0;
}
P3674 小清新人渣的本愿
由于本题没有修改操作,故可以使用离线做法,考虑莫队。
假设当前序列中存在一对
考虑加法,若当前序列中存在一对
对于乘法,我们只需
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <bitset>
using namespace std;
namespace IO
{
template<typename T>
inline void input(T &x)
{
x = 0;
register char ch = getchar();
register bool f = false;
while (!isdigit(ch))
{
f |= (ch == '-');
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = f ? -x : x;
}
}
using namespace IO;
namespace Primary
{
const int maxn = 1e5 + 10;
int n, m, a[maxn], cnt[maxn], tot;
bool ans[maxn];
bitset<maxn> pre, suf;
struct event
{
int opt, s, e, v, id;
bool operator < (const event &rhs) const
{
return (s / tot) ^ (rhs.s / tot) ? s < rhs.s :
((s / tot) & 1) ? e < rhs.e : e > rhs.e;
}
}q[maxn];
inline void add(int x)
{
x = a[x];
if (!(cnt[x]++)) pre[x] = suf[n - x] = true;//在指针扫动时直接维护 bitset 即可
}
inline void del(int x)
{
x = a[x];
if (!(--cnt[x])) pre[x] = suf[n - x] = false;
}
void main()
{
input(n), input(m);
tot = sqrt(n);
for (int i = 1; i <= n; ++i)
input(a[i]);
for (int i = 1, opt, s, e, x; i <= m; ++i)
{
input(q[i].opt), input(q[i].s), input(q[i].e), input(q[i].v);
q[i].id = i;
}
sort(q + 1, q + m + 1);
int L = q[1].s, R = L - 1, ql, qr, x;
for (int i = 1; i <= m; ++i)
{
ql = q[i].s, qr = q[i].e;
while (ql > L) del(L++);
while (ql < L) add(--L);
while (qr > R) add(++R);
while (qr < R) del(R--);
x = q[i].v;
if (q[i].opt == 1)
ans[q[i].id] = (pre & (pre << x)).any();
else if (q[i].opt == 2)
ans[q[i].id] = (pre & (suf >> (n - x))).any();
else
{
for (int j = 1; j * j <= x; ++j)
{
if (x % j != 0) continue;//注意去除不是 x 的约数
if (pre[j] && pre[x / j])
{
ans[q[i].id] = true;
break;
}
}
}
}
for (int i = 1; i <= m; ++i)
puts(ans[i] ? "hana" : "bi");
}
}
int main()
{
Primary::main();
return 0;
}
P6328 我是仙人掌
注意到
由于题目中询问的是
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <queue>
using namespace std;
namespace Primary
{
const int inf = 1005;
int n, m, q, dis[1010][1010];
bitset<1010> f[1010][1010];
vector<int> e[1010];
void bfs(int S, int *dis)
{
for (int i = 1; i <= n; ++i) dis[i] = inf;//将 dis 初始化为 inf
queue<int> q;
q.push(S);
dis[S] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
for (unsigned i = 0, v; i < e[u].size(); ++i)
{
v = e[u][i];
if (dis[v] == inf)
{
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
for (int i = 1; i <= n; ++i) f[S][dis[i]].set(i);
for (int i = 1; i <= n; ++i) f[S][i] |= f[S][i - 1];
}
void main()
{
scanf("%d%d%d", &n, &m, &q);
for (int i = 1, u, v; i <= m; ++i)
{
scanf("%d%d", &u, &v);
e[u].push_back(v), e[v].push_back(u);
}
for (int i = 1; i <= n; ++i) bfs(i, dis[i]);
while (q--)
{
bitset<1010> ans;
int u, v, x;
scanf("%d", &x);
while (x--)
{
scanf("%d%d", &u, &v);
if (v > n) v = n;//注意一个 n 个点的图中最短路长度不会超过 n
ans |= f[u][v];
}
printf("%d\n", ans.count());
}
}
}
int main()
{
Primary::main();
return 0;
}
CF914F Substrings in a String
感谢 @ywy_c_asm 学长推荐本题 w!
本题看上去像是 kmp 动态修改,但实际上是 bitset 暴力 QwQ
题目中给定的字符串只会出现 26 种字符(即所有小写字母),故考虑开 26 个 bitset 分别记录 26 种字符在
那么修改操作(即 1 操作)可直接在对应 bitset 中修改即可。
考虑询问操作(即 2 操作),发现两个串匹配的过程(下文设询问给出的字符串为
注意本篇代码的字符串下标是从 0 开始的,而题目中的字符串下标是从 1 开始的。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
namespace Primary
{
const int maxn = 1e5 + 10;
char s[maxn], t[maxn];
int q;
bitset<maxn> help[26], ans;
void main()
{
scanf("%s%d", s, &q);
int len = strlen(s);
for (int i = 0; i < len; ++i)
help[s[i] - 'a'].set(i);
int opt, ql, qr;
while (q--)
{
scanf("%d", &opt);
if (opt == 1)
{
scanf("%d%s", &ql, t), --ql;
help[s[ql] - 'a'].set(ql, 0);
help[t[0] - 'a'].set(ql);
s[ql] = t[0];
}
else
{
scanf("%d%d%s", &ql, &qr, t);
len = strlen(t);
if (len > qr - ql + 1)//若 t 的长度大于询问长度,显然无解
{
puts("0");
continue;
}
ans.set();
for (int i = 0; i < len; ++i)
ans &= (help[t[i] - 'a'] >> i);
int ans_l = (ans >> (ql - 1)).count();
int ans_r = (ans >> (qr - len + 1)).count();
printf("%d\n", ans_l - ans_r);
}
}
}
}
int main()
{
Primary::main();
return 0;
}
P5640 【CSGRound2】逐梦者的初心
多亏了前一道题的铺垫,让我想到了本题
然而比赛时没调出来QAQ
感觉题目中的式子比较难受 >_<,故考虑将其转化成偏移量的形式:
初始给定一个字符串
s 和一个空串t 。每次操作会在t 的首或尾插入一个字符。每次插入后询问存在多少个j \in [0, |t| - 1] 使得对于每一个i \in [1, |t| - j] ,都满足s_i \neq t_{i + j} 。
发现对于每个
统计一次答案我们只需扫描一遍序列即可,我们发现这个过程可以像前一道题一样使用 bitset 进行优化,然而这样可能导致我们在修改
这正启发我们关注每一次修改时会对答案造成什么样的影响:
对于在
对于在
注意只有
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
template<typename T>
inline bool chkmax(T& x, const T& y) {return x < y ? (x = y, true) : false;}
namespace IO
{
template<typename T>
inline void input(T& x)
{
x = 0;
register char ch = getchar();
register bool f = false;
while (!isdigit(ch))
{
f |= (ch == '-');
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = f ? -x : x;
}
}
using IO::input;
namespace Primary
{
int n, m;
bitset<35000> id[1010], now, base;
void main()
{
input(n), input(m);
register int i, x, opt;
for (i = 1; i <= n; ++i)
{
input(x);
if (i <= m) id[x].set(i);
}
base.set();
for (i = 1; i <= m; ++i)
{
input(opt), input(x);
base.set(i, 0);//限制对答案产生贡献的长度
if (opt == 0) now = (now << 1) | id[x];
else now |= id[x] << (i - 1);
printf("%llu\n", (~(now | base)).count());
}
}
}
int main()
{
Primary::main();
return 0;
}