版本比较
莫队学习笔记
正文
增加 6 行删除 103 行
113 行未改动全部展开11411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524641 行未改动288289290291292293294295296297113 行未改动全部展开注意到 $a$ 的范围可能很大,因此需要离散化。::::success[code]```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 2e5 + 1;int n, m, x, a[N], val[N], cnt, nxt[N];int mp[N], Ans, ans[N], pos[N], len;struct query{int l, r, id;bool operator < (query &o) const{if(pos[l] != pos[o.l]) return l < o.l;if(pos[l] & 1) return r < o.r;return r > o.r;//奇偶优化好习惯}}q[N];void add(int p){Ans += mp[nxt[p]];mp[a[p]]++;}void del(int p){mp[a[p]]--;Ans -= mp[nxt[p]];}//莫队移动操作signed main(){ios::sync_with_stdio(0), cin.tie(0);cin >> n >> m >> x;len = sqrt(n);for(int i = 1; i <= n; i++){cin >> a[i];val[++cnt] = a[i], val[++cnt] = a[i] ^ x;pos[i] = (i - 1) / len + 1;}sort(val + 1, val + cnt + 1);cnt = unique(val + 1, val + cnt + 1) - (val + 1);for(int i = 1; i <= n; i++){nxt[i] = lower_bound(val + 1, val + cnt + 1, a[i] ^ x) - val;a[i] = lower_bound(val + 1, val + cnt + 1, a[i]) - val;}//离散化for(int i = 1; i <= m; i++){cin >> q[i].l >> q[i].r;q[i].id = i;}sort(q + 1, q + m + 1);for(int i = 1, l = 1, r = 0; i <= m; i++){while(l > q[i].l) add(--l);while(r < q[i].r) add(++r);while(l < q[i].l) del(l++);while(r > q[i].r) del(r--);//先插入后删除!!ans[q[i].id] = Ans;}for(int i = 1; i <= m; i++)cout << (ans[i] > 0 ? "yes" : "no") << '\n';return 0;}```::::### [例题2](https://www.luogu.com.cn/problem/P4396)普通的维护每个值出现的次数就是上面的板子。如果还需要区间求和,就需要带上一个数据结构支持单点加,区间和的操作,考虑树状数组。::::success[code]```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 1e5 + 1;int n, m, a[N], cnt[N], ans[N][2];int len, pos[N];struct query{int l, r, A, B, id;bool operator < (query &o){//自己写}}q[N];struct Bit{int c[N];void init(int x){fill(c + 1, c + x + 1, 0);}void add(int x, int k){for(; x < N; x += (x & (-x))) c[x] += k;}int sum(int x){int ans = 0; for(; x; x -= (x & (-x))) ans += c[x]; return ans;}}bit;void add(int x){if(!cnt[x]) bit.add(x, 1);cnt[x]++;}void del(int x){cnt[x]--;if(!cnt[x]) bit.add(x, -1);}signed main(){ios::sync_with_stdio(0), cin.tie(0);///读入for(int i = 1, l = 1, r = 0; i <= m; i++){while(l > q[i].l) add(a[--l]);while(r < q[i].r) add(a[++r]);while(l < q[i].l) del(a[l++]);while(r > q[i].r) del(a[r--]);ans[q[i].id][1] = bit.sum(q[i].B) - bit.sum(q[i].A - 1);}bit.init(N - 1);for(int i = 1, l = 1, r = 0; i <= m; i++){while(l > q[i].l) bit.add(a[--l], 1);while(r < q[i].r) bit.add(a[++r], 1);while(l < q[i].l) bit.add(a[l++], -1);while(r > q[i].r) bit.add(a[r--], -1);ans[q[i].id][0] = bit.sum(q[i].B) - bit.sum(q[i].A - 1);}for(int i = 1; i <= m; i++) cout << ans[i][0] << ' ' << ans[i][1] << '\n';return 0;}```::::### 习题[P1997](https://www.luogu.com.cn/problem/P1997)[P1494](https://www.luogu.com.cn/problem/P1494)[P4688](https://www.luogu.com.cn/problem/P4688)## 配合 `bitset` 的使用莫队配合 `bitset` 是莫队的基本题型,lxl 或许很喜欢?([先放例题](https://www.luogu.com.cn/problem/P4137)。41 行未改动::::看起来很简单,考虑使用 `bitset` 维护每一个数是否出现。利用 `bitset._Find_first()` 函数来求 $\operatorname{mex}$,时间复杂度 $O(n\sqrt{n}+\frac{nq}w)$ 理论上过不了,但是远远跑不满。[习题1](https://www.luogu.com.cn/problem/P3674),[习题2](https://www.luogu.com.cn/problem/P5355)。# 带修莫队# 回滚莫队# 树上莫队# 莫队二次离线## 后记113 行未改动全部展开11411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714841 行未改动190191192193194195196197198199200113 行未改动全部展开注意到 $a$ 的范围可能很大,因此需要离散化。::::success[code]```cppvoid add(int p){Ans += mp[nxt[p]];mp[a[p]]++;}// nxt[p] 表示 p^x 离散化后的值void del(int p){mp[a[p]]--;Ans -= mp[nxt[p]];}//莫队移动操作```::::### [例题2](https://www.luogu.com.cn/problem/P4396)普通的维护每个值出现的次数就是上面的板子。如果还需要区间求和,就需要带上一个数据结构支持单点加,区间和的操作,考虑树状数组。::::success[code]```cppvoid add(int x){if(!cnt[x]) bit.add(x, 1);cnt[x]++;}void del(int x){cnt[x]--;if(!cnt[x]) bit.add(x, -1);}//第二问```::::### 习题[P1997](https://www.luogu.com.cn/problem/P1997)[P1494](https://www.luogu.com.cn/problem/P1494)## 配合 `bitset` 的使用莫队配合 `bitset` 是莫队的基本题型,lxl 或许很喜欢?([先放例题](https://www.luogu.com.cn/problem/P4137)。41 行未改动::::看起来很简单,考虑使用 `bitset` 维护每一个数是否出现。利用 `bitset._Find_first()` 函数来求 $\operatorname{mex}$,时间复杂度 $O(n\sqrt{n}+\frac{nq}w)$ 理论上过不了,但是远远跑不满。[P3647](https://www.luogu.com.cn/problem/P3674)[P5355](https://www.luogu.com.cn/problem/P5355)[P4688](https://www.luogu.com.cn/problem/P4688)# 带修莫队# 回滚莫队# 树上莫队## 后记