1 条题解
-
0
与集合运算 2思路大致相同,可以利用
bitset
优化。每个元素都加上/减去等价于左移/右移;对称差等价于异或运算。使用
bitset
的一个坑点,便是bitset
在左移时只会将超出类模板参数中设置的字节大小的部分删去,而题目要求的是的都删去。所以可以创建一个掩码mask
,将除外的所有为设为true
,每次左移后与掩码进行一次与运算即可。#include <iostream> #include <bitset> using namespace std; int n, m, q; bitset<30000> mask; // 掩码 bitset<30000> s[30005]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m >> q; mask.set(); for (int i = m; i < mask.size(); ++i) mask[i] = false; for (int i = 1; i <= n; ++i) { int c; cin >> c; while (c--) { int x; cin >> x; s[i][x - 1] = true; } } while (q--) { int type, x, y; cin >> type >> x >> y; if (type == 1) { s[x] <<= y; s[x] &= mask; } else if (type == 2) s[x] >>= y; else if (type == 3) cout << (s[x] & s[y]).count() << endl; else if (type == 4) cout << (s[x] | s[y]).count() << endl; else cout << (s[x] ^ s[y]).count() << endl; } return 0; }
- 1
信息
- ID
- 8167
- 时间
- 500ms
- 内存
- 512MiB
- 难度
- 3
- 标签
- 递交数
- 3
- 已通过
- 2
- 上传者