『学习笔记』莫队
Part 0. 目录
- 概念
- 普通莫队
- 树上莫队
- 带修莫队
Part 1. 概念
莫队是由莫涛提出的算法。莫队算法可以解决一类离线区间询问问题,适用性极为广泛。
Part 2. 普通莫队
普通莫队主要针对于多次区间询问的问题,基于分块的思想。
过程如下:
先将当前区间
调用的代码如下:
inline void add(int x) {
//...
}
inline void del(int x) {
//...
}
sort(q + 1, q + m + 1);
int l = 1, r = 0;
for(int i = 1; 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] = sum;
}
而排序怎么排以及块长
一般的排序方式是先按左端点所在块为第一关键字,再按右端点为第二关键字排序,代码如下:
return b[l] != b[x.l] ? l < x.l : r < x.r;
但对于这种:
画框部分就多移动了两遍,于是我们考虑奇偶化排序,也就是若左端点所在块相等,则若为奇数块,则按右端点升序排序;否则按右端点降序排序。
代码如下:
return b[l] != b[x.l] ? l < x.l : (r == x.r ? 0 : (b[l] & 1) ^ (r < x.l));
此时排序情况如下,很明显减少了一倍的移动量。
时间复杂度为
块长一般取
特别地,在一般的题里,
接着便是一堆例题。
SP3267 & P1972
题目链接:link1 and link2。
题目大意:每次询问求区间
题目的关键在于
对于一个数
同理,在它刚离开区间
两个函数代码如下:
inline void add(int x) { sum += !cnt[x]++; }
inline void del(int x) { sum -= !--cnt[x]; }
PS:P1972 需要卡常,莫队并非正解,但卡的过去
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n, m, c[N], bl, b[N], sum = 0, cnt[N], ans[N];
struct node {
int l, r, id;
bool operator < (const node &x) const {
if(b[l] != b[x.l]) { return b[l] < b[x.l]; }
return r < x.r;
}
} a[N];
inline void add(int x) { sum += !cnt[x]++; }
inline void del(int x) { sum -= !--cnt[x]; }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
bl = sqrt(n + 1);
for(int i = 1; i <= n; i++) {
cin >> c[i];
b[i] = (i - 1) / bl + 1;
}
cin >> m;
for(int i = 1; i <= m; i++) {
cin >> a[i].l >> a[i].r;
a[i].id = i;
}
sort(a + 1, a + m + 1);
int l = 1, r = 0;
for(int i = 1; i <= m; i++) {
while(l > a[i].l) { add(c[--l]); }
while(r < a[i].r) { add(c[++r]); }
while(l < a[i].l) { del(c[l++]); }
while(r > a[i].r) { del(c[r--]); }
ans[a[i].id] = sum;
}
for(int i = 1; i <= m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
P1494
题目链接:link。
题目大意:令区间
同样是两个函数,但很好处理,每次
P2709
题目链接:link。
题目大意:求
对于每次移动,
CF1000F
题目链接:link。
题目大意:求任意一个
用一个栈来存满足条件的
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, m, a[N], bl, sum = 0, cnt[N], ans[N];
int b[N], c[N], tot;
struct node {
int l, r, b, id;
bool operator < (const node &x) const {
if(b != x.b) { return b < x.b; }
return r == x.r ? 0 : (b & 1) ^ (r < x.r);
}
} q[N];
inline void add(int x) {
++cnt[x];
if(cnt[x] == 1) {
++sum;
b[++tot] = x;
c[x] = tot;
}
if(cnt[x] == 2) {
--sum;
b[c[x]] = b[tot];
c[b[tot]] = c[x];
b[tot--] = c[x] = 0;
}
}
inline void del(int x) {
--cnt[x];
if(cnt[x] == 1) {
++sum;
b[++tot] = x;
c[x] = tot;
}
if(cnt[x] == 0) {
--sum;
b[c[x]] = b[tot];
c[b[tot]] = c[x];
b[tot--] = c[x] = 0;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
bl = sqrt(n);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> m;
for(int i = 1; i <= m; i++) {
cin >> q[i].l >> q[i].r;
q[i].b = q[i].l / bl, q[i].id = i;
}
sort(q + 1, q + m + 1);
int l = 1, r = 0;
for(int i = 1; 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--]); }
if(sum) {
ans[q[i].id] = b[tot];
}
}
for(int i = 1; i <= m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
CF220B
题目链接:link。
题目大意:有多少个
对于
两个函数代码如下:
inline void add(int x) {
if(x > n) { return ; }
++cnt[x];
if(cnt[x] == x) { ++sum; }
if(cnt[x] == x + 1) { --sum; }
}
inline void del(int x) {
if(x > n) { return ; }
--cnt[x];
if(cnt[x] == x) { ++sum; }
if(cnt[x] == x - 1) { --sum; }
}
Part 3. 树上莫队
对于树上路径问题,通常是用欧拉序转换为线性,还要加上 LCA,所以代码通常会很长。对于子树的问题,通常用 dfn 序转换。
欧拉序:在第一次遍历到
SP10707
题目链接:link。
题目大意:求
首先
既然是求路径不难想到 LCA 来
而在询问时的
如果
至于求 LCA,由于我不会树剖,所以就用普通的倍增求就可以了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, bl, a[N], c[N], cnt[N], ans[N], sum;
int f[N][20], dep[N], eu[N], tot, s[N], t[N];
bool vis[N];
vector<int> e[N];
struct node {
int l, r, id, lca;
bool operator < (const node &x) const {
return l / bl != x.l / bl ? l / bl < x.l / bl : r > x.r;
}
} q[N];
inline void dfs(int x, int fa) {
dep[x] = dep[fa] + 1, eu[s[x] = ++tot] = x;
f[x][0] = fa;
for(int i = 1; i < 20; i++) {
f[x][i] = f[f[x][i - 1]][i - 1];
}
for(int i : e[x]) {
if(i == fa) { continue; }
dfs(i, x);
}
eu[t[x] = ++tot] = x;
}
inline int lca(int x, int y) {
if(x == y) { return x; }
if(dep[x] < dep[y]) { swap(x, y); }
for(int i = 19; i >= 0; i--) {
while(dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
if(x == y) { return x; }
}
for(int i = 19; i >= 0; i--) {
if(f[x][i] != f[y][i]) {
x = f[x][i], y = f[y][i];
}
}
return f[x][0];
}
inline void solve(int x) {
if(!(vis[x] ^= 1) && !--cnt[a[x]]) { --sum; }
if(vis[x] && !cnt[a[x]]++) { ++sum; }
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
bl = sqrt(m);
for(int i = 1; i <= n; i++) {
cin >> a[i];
c[i] = a[i];
}
sort(c + 1, c + n + 1);
int tmp = unique(c + 1, c + n + 1) - c - 1;
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(c + 1, c + tmp + 1, a[i]) - c;
}
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1, 0);
for(int i = 1, x, y; i <= m; i++) {
cin >> x >> y;
q[i].id = i;
int tmp = lca(x, y);
if(tmp == x || tmp == y) {
if(s[x] > s[y]) { swap(x, y); }
q[i].l = s[x], q[i].r = s[y];
} else {
if(s[x] > t[y]) { swap(x, y); }
q[i].l = t[x], q[i].r = s[y];
q[i].lca = tmp;
}
}
sort(q + 1, q + m + 1);
int l = 1, r = 0;
for(int i = 1; i <= m; i++) {
while(l > q[i].l) { solve(eu[--l]); }
while(r < q[i].r) { solve(eu[++r]); }
while(l < q[i].l) { solve(eu[l++]); }
while(r > q[i].r) { solve(eu[r--]); }
if(q[i].lca) { solve(q[i].lca); }
ans[q[i].id] = sum;
if(q[i].lca) { solve(q[i].lca); }
}
for(int i = 1; i <= m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
CF375D
题目链接:link
题目大意:求
想到用 dfn 序转换为线性,对于节点
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct node {
int x, id, dfn, r;
bool operator < (const node &p) const {
return dfn < p.dfn;
}
} a[N];
int n, m, bl, b[N], cnt[N], sum[N], ans[N], tot;
vector<int> e[N];
struct node1 {
int l, r, k, id;
bool operator < (const node1 &x) const {
return b[l] != b[x.l] ? b[l] < b[x.l] : r < x.r;
}
} q[N];
inline void dfs(int x, int fa) {
a[x].dfn = ++tot;
for(int i : e[x]) {
if(i == fa) { continue; }
dfs(i, x);
}
a[x].r = tot;
}
inline void add(int x) { ++sum[++cnt[x]]; }
inline void del(int x) { --sum[cnt[x]--]; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
bl = sqrt(n);
for(int i = 1; i <= n; i++) {
cin >> a[i].x;
a[i].id = i;
b[i] = (i - 1) / bl + 1;
}
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1, 0);
for(int i = 1; i <= m; i++) {
int u, k;
cin >> u >> k;
q[i] = {a[u].dfn, a[u].r, k, i};
}
sort(a + 1, a + n + 1);
sort(q + 1, q + m + 1);
int l = 1, r = 0;
for(int i = 1; i <= m; i++) {
while(l > q[i].l) { add(a[--l].x); }
while(r < q[i].r) { add(a[++r].x); }
while(l < q[i].l) { del(a[l++].x); }
while(r > q[i].r) { del(a[r--].x); }
ans[q[i].id] = sum[q[i].k];
}
for(int i = 1; i <= m; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Part 4. 带修莫队
我们让莫队修改,可以强行加上一维时间维表示此时修改的次数,所以
转移同样是
return b[l] != b[x.l] ? l < x.l : (b[r] != b[x.r] ? r < x.r : t < x.t);
但是块长就不是
下面是 OI-wiki 上给的时间复杂度证明。
我们设序列长为
n ,m 个询问,t 个修改。 带修莫队排序的第二关键字是右端点所在块编号,不同于普通莫队。 想一想,如果不把右端点分块:
- 乱序的右端点对于每个询问会移动
n 次。- 有序的右端点会带来乱序的时间,每次询问会移动
t 次。无论哪一种情况,带来的时间开销都无法接受。 接下来分析时间复杂度。 设块长为
s ,则有\dfrac{n}{s} 个块。对于块i 和块j ,记有q_{i,j} 个询问的左端点位于块i ,右端点位于块j 。 每「组」左右端点不换块的询问(i,j) ,端点每次移动O(s) 次,时间单调递增,O(t) 。 左右端点换块的时间忽略不计。 表示一下就是:\begin{aligned}&\sum_{i=1}^{n/s}\sum_{j=i+1}^{n/s}(q_{i,j}\cdot s+t)\\=&ms+\left(\dfrac{n}{s}\right)^2t\\=&ms+\dfrac{n^2t}{s^2}\end{aligned} 考虑求导求此式极小值。设
f(s)=ms+\dfrac{n^2t}{s^2} 。那f'(s)=m-\dfrac{2n^2t}{s^3}=0 。 得s=\sqrt[3]{\dfrac{2n^2t}{m}}=\dfrac{2^{1/3}n^{2/3}t^{1/3}}{m^{1/3}}=s_0 。 也就是当块长取\dfrac{n^{2/3}t^{1/3}}{m^{1/3}} 时有最优时间复杂度O\left(n^{2/3}m^{2/3}t^{1/3}\right) 。 常说的O\left(n^{5/3}\right) 便是把n,m,t 当做同数量级的时间复杂度。 实际操作中还是推荐设定n^{2/3} 为块长。
P1903 & UVA12345
题目链接:link1 and link2。
题目大意:两种询问:
-
Q l r:查询[l,r] 内权值种数。 -
R p c:将a_p 修改为c 。
板子题,不过是加了修改
while(t < q[i].t) {
++t;
if(q1[t].p >= l && q1[t].p <= r) {
del(a[q1[t].p]), add(q1[t].c);
}
swap(a[q1[t].p], q1[t].c);
}
while(t > q[i].t) {
if(q1[t].p >= l && q1[t].p <= r) {
del(a[q1[t].p]), add(q1[t].c);
}
swap(a[q1[t].p], q1[t].c);
--t;
}
SP30906
题目链接:link。
题目大意:将上一题的查询改为
修改一下
SP32952
题目链接:link。
题目大意:查询操作为:令
树上带修莫队,求一下欧拉序转换为线性再用带修莫队就可以了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N], bl, b[N], cnt[N];
int tot1, tot2, vis[N], sum, ans[N];
struct node {
int l, r, t, lca, id;
bool operator < (const node &x) const {
return b[l] != b[x.l] ? b[l] < b[x.l] : (b[r] != b[x.r] ? b[r] < b[x.r] : t < x.t);
}
} q[N];
struct node1 {
int p, c;
} q1[N];
int dep[N], eu[N << 1], tot, f[N][25], s[N], t[N];
vector<int> e[N];
inline void dfs(int x, int fa) {
dep[x] = dep[fa] + 1, eu[s[x] = ++tot] = x;
f[x][0] = fa;
for(int i = 1; i <= 20; i++) {
f[x][i] = f[f[x][i - 1]][i - 1];
}
for(int i : e[x]) {
if(i == fa) { continue; }
dfs(i, x);
}
eu[t[x] = ++tot] = x;
}
inline int lca(int x, int y) {
if(x == y) { return x; }
if(dep[x] < dep[y]) { swap(x, y); }
for(int i = 20; i >= 0; i--) {
while(dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
if(x == y) { return x; }
}
for(int i = 19; i >= 0; i--) {
if(f[x][i] != f[y][i]) {
x = f[x][i], y = f[y][i];
}
}
return f[x][0];
}
inline void add(int x) {
sum -= cnt[x] * (cnt[x] - 1) / 2;
++cnt[x];
sum += cnt[x] * (cnt[x] - 1) / 2;
}
inline void del(int x) {
sum -= cnt[x] * (cnt[x] - 1) / 2;
--cnt[x];
sum += cnt[x] * (cnt[x] - 1) / 2;
}
inline void solve(int x) {
(vis[x] ^= 1) ? add(a[x]) : del(a[x]);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
bl = pow(n * 2, 2.0 / 3);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= (n << 1); i++) {
b[i] = (i - 1) / bl + 1;
}
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
++u, ++v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1, 0);
for(int i = 1; i <= m; i++) {
int op, x, y;
cin >> op >> x >> y;
if(op == 1) { q1[++tot2] = {x + 1, y}; }
else {
++x, ++y, ++tot1;
if(s[x] > s[y]) { swap(x, y); }
int tmp = lca(x, y);
if(tmp == x) {
q[tot1].l = s[x];
} else {
q[tot1].l = t[x], q[tot1].lca = tmp;
}
q[tot1].r = s[y], q[tot1].id = tot1, q[tot1].t = tot2;
}
}
sort(q + 1, q + tot1 + 1);
int l = 1, r = 0, t = 0;
for(int i = 1; i <= tot1; i++) {
while(l > q[i].l) { solve(eu[--l]); }
while(r < q[i].r) { solve(eu[++r]); }
while(l < q[i].l) { solve(eu[l++]); }
while(r > q[i].r) { solve(eu[r--]); }
while(t < q[i].t) {
++t;
if(vis[q1[t].p]) {
del(a[q1[t].p]), add(q1[t].c);
}
swap(a[q1[t].p], q1[t].c);
}
while(t > q[i].t) {
if(vis[q1[t].p]) {
del(a[q1[t].p]), add(q1[t].c);
}
swap(a[q1[t].p], q1[t].c);
--t;
}
if(q[i].lca) { solve(q[i].lca); }
ans[q[i].id] = sum;
if(q[i].lca) { solve(q[i].lca); }
}
for(int i = 1; i <= tot1; i++) {
cout << ans[i] << '\n';
}
return 0;
}
CF940F
题目链接:link。
题目大意:求
先离散化
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, a[N], bl, b[N], cnt[N], sum[N], ans[N], tot1, tot2;
int c[N], tot;
struct node {
int l, r, id, t;
bool operator < (const node &x) const {
return b[l] != b[x.l] ? l < x.l : (b[r] != b[x.r] ? r < x.r : t < x.t);
}
} q[N];
struct node1 {
int p, c;
} q1[N];
inline void add(int x) {
--sum[cnt[x]];
++sum[++cnt[x]];
}
inline void del(int x) {
--sum[cnt[x]];
++sum[--cnt[x]];
}
inline void solve(int x, int l, int r) {
if(q1[x].p >= l && q1[x].p <= r) {
del(a[q1[x].p]), add(q1[x].c);
}
swap(a[q1[x].p], q1[x].c);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
bl = pow(n, 2.0 / 3);
for(int i = 1; i <= n; i++) {
cin >> a[i];
c[++tot] = a[i];
b[i] = (i - 1) / bl + 1;
}
for(int i = 1; i <= m; i++) {
int opt, l, r;
cin >> opt >> l >> r;
if(opt == 1) {
q[++tot1] = {l, r, tot1, tot2};
} else {
q1[++tot2] = {l, r};
c[++tot] = r;
}
}
sort(c + 1, c + tot + 1);
tot = unique(c + 1, c + tot + 1) - c - 1;
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(c + 1, c + tot + 1, a[i]) - c;
}
for(int i = 1; i <= tot2; i++) {
q1[i].c = lower_bound(c + 1, c + tot + 1, q1[i].c) - c;
}
sort(q + 1, q + tot1 + 1);
int l = 1, r = 0, t = 0;
for(int i = 1; i <= tot1; 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--]); }
while(t < q[i].t) { solve(++t, q[i].l, q[i].r); }
while(t > q[i].t) { solve(t--, q[i].l, q[i].r); }
ans[q[i].id] = 1;
while(sum[ans[q[i].id]]) {
++ans[q[i].id];
}
}
for(int i = 1; i <= tot1; i++) {
cout << ans[i] << '\n';
}
return 0;
}
P4074
题目链接:link。
题目大意:令
板子题,修改
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int s[N], t[N], c1[N], ans[N], sum;
int dep[N], f[N][25], eu[N], v[N], w[N];
int bl, tot, n, m, Q;
int b[N], c[N], cnt[N];
bool vis[N];
vector<int> e[N];
int tot1 = 0, tot2 = 0;
struct query {
int l, r, t, id;
bool operator < (const query &x) const {
return b[l] != b[x.l] ? l < x.l : (b[r] != b[x.r] ? r < x.r : t < x.t);
}
} q[N], q1[N];
inline void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
eu[s[x] = ++tot] = x;
f[x][0] = fa;
for(int i = 1; i <= 20; i++) {
f[x][i] = f[f[x][i - 1]][i - 1];
}
for(int i : e[x]) {
if(i == fa) { continue; }
dfs(i, x);
}
eu[t[x] = ++tot] = x;
}
inline int lca(int x, int y) {
if(x == y) { return x; }
if(dep[x] < dep[y]) { swap(x, y); }
for(int i = 19; i >= 0; i--) {
while(dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
if(x == y) { return x; }
}
for(int i = 19; i >= 0; i--) {
if(f[x][i] != f[y][i]) {
x = f[x][i], y = f[y][i];
}
}
return f[x][0];
}
inline void solve(int x) {
if(vis[x]) {
sum -= v[c[x]] * w[cnt[c[x]]--];
} else {
sum += v[c[x]] * w[++cnt[c[x]]];
}
vis[x] ^= 1;
}
inline void modify(int x, int t) {
if(vis[x]) {
solve(x);
c[x] = t;
solve(x);
} else {
c[x] = t;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> Q;
for(int i = 1; i <= m; i++) {
cin >> v[i];
}
for(int i = 1; i <= n; i++) {
cin >> w[i];
}
for(int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
for(int i = 1; i <= n; i++) {
cin >> c1[i];
c[i] = c1[i];
}
dfs(1, 0);
bl = pow(tot, 2.0 / 3);
for(int i = 1; i <= (n << 1); i++) {
b[i] = (i - 1) / bl + 1;
}
while(Q--) {
int opt, x, y;
cin >> opt >> x >> y;
if(!opt) {
q1[++tot2].l = x;
q1[tot2].r = c1[x];
c1[x] = q1[tot2].t = y;
} else {
if(s[x] > s[y]) swap(x, y);
int tmp = lca(x, y);
q[++tot1] = {tmp == x ? s[x] : t[x], s[y], tot2, tot1};
}
}
sort(q + 1, q + tot1 + 1);
int l = 1, r = 0, t = 0;
for(int i = 1; i <= tot1; i++) {
while(l > q[i].l) { solve(eu[--l]); }
while(r < q[i].r) { solve(eu[++r]); }
while(l < q[i].l) { solve(eu[l++]); }
while(r > q[i].r) { solve(eu[r--]); }
while(t < q[i].t) { ++t, modify(q1[t].l, q1[t].t); }
while(t > q[i].t) { modify(q1[t].l, q1[t].r), --t; }
int x = eu[l], y = eu[r];
int tmp = lca(x, y);
if(x != tmp && y != tmp) {
solve(tmp);
ans[q[i].id] = sum;
solve(tmp);
} else { ans[q[i].id] = sum; }
}
for(int i = 1; i <= tot1; i++) {
cout << ans[i] << '\n';
}
return 0;
}