跳到正文

算法·理论

算法总结——普通莫队

program_xwl约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年3月23日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 13 分钟

版本

  1. v1当前

莫队是一种分块思想衍生的离线算法,适合处理区间信息问题。

莫队算法

莫队的分类

  • 普通莫队:不带修改,只有询问。
  • 带修莫队:有修改也有询问。
  • 回滚莫队
  • 树上莫队
  • 二次离线莫队
  • ······

这里只讲解普通莫队。

标准问题

给定一个长度为 nn 的序列 aa,有 TT 次询问,每次询问一个区间 [pl,pr][pl,pr],问区间中有多少个不同的数。

思路

将序列分成 n\sqrt{n} 块,把询问按照左端点所在块的序号从小到大排序,如果两左端点在同一个块内,按右端点从小到大排序。接着开两个指针 ll 和 rr,分别指向当前已经处理答案的区间的左右端点。每次移动 ll 和 rr,直到与下一个询问的区间的左右端点重合。做法就是这样,给人一种时间复杂度 O(n2)O(n^2) 的感觉,但实际上是 O(nn)O(n\sqrt{n}),下面给出证明。

时间复杂度

分考虑移动左端点和移动右端点的时间消耗

左端点

左端点在跨块移动时,根据排序规则,不同块的询问左端点相对关系一定是有序的,所以该部分为 O(n)O(n)。

左端点在块内移动时,根据排序规则,左端点此时是无序的,可能出现左右横跳的情况。但它们在同一个块内,最多走为 n\sqrt{n} 次,所以该部分为 O(mn)O(m\sqrt{n})。

右端点

在(左端点的)同一个块内,根据排序规则,这种情况下右端点是有序的,最多走 O(n)O(n) 次。共有 n\sqrt{n} 个块,综合就是 O(nn)O(n\sqrt{n})。

综上所述,普通莫队的时间复杂度为 O((n+m)n)O((n+m)\sqrt{n})。

奇偶优化

当 rr 跨块移动时,需要从前一个块的最大值跑到后一个块的最小值,这个过程不能计算什么答案,但计算量较大。奇偶优化就是把这一段长距离移动用来计算答案。如果一个右端点对应的左端点所属的块的编号为偶数,那么它从小到大排序;若为奇数,则从大到小排序。这就可以把这段本来不能计算答案长距离移动用来计算一个块的答案。虽然时间复杂度还是 O((n+m)n)O((n+m)\sqrt{n}),但常数至少可以少一半。随机数据优化更多。如下图。

代码

C++
struct problem {int l,r,id;} problems[50005];//存储询问
long long n,m,k,a[50005],block_siz,the_ans = 0,ans[50005],cnt[50005];//a为原序列;block_siz为块的大小;the_ans表示当前指针指向的区间的答案;ans存储每个询问的答案;cnt记录每个数的出现次数

bool cmp(problem x,problem y)//排序函数
{
	if(x.l/block_siz != y.l/block_siz) return x.l < y.l;
	if((y.l/block_siz)&1) return x.r < y.r;//奇偶优化
	return x.r > y.r;
}

void add(int x)//将x计入答案
{
	the_ans -= cnt[a[x]]*cnt[a[x]];
	cnt[a[x]]++;
	the_ans += cnt[a[x]]*cnt[a[x]];
	return;
}

void del(int x)//将x移除答案
{
	the_ans -= cnt[a[x]]*cnt[a[x]];
	cnt[a[x]]--;
	the_ans += cnt[a[x]]*cnt[a[x]];
	return;
}

int main()
{
    ...
    block_siz = sqrt(n);
	for(int i = 1;i <= m;i++) problems[i].id = i;//记录原始位置
	sort(problems+1,problems+m+1,cmp);//对询问序列进行排序
	int l = problems[1].l,r = problems[1].l-1;//分别指向答案区间的两个端点
	for(int i = 1;i <= m;i++)
	{
		int pl = problems[i].l,pr = problems[i].r;//询问区间
		while(pl < l) add(--l);//将l移到询问的pl
		while(r < pr) add(++r);//将r移到询问的pr
		while(l < pl) del(l++);//删除左边多余的部分
		while(pr < r) del(r--);//删除右边多余的部分
		ans[problems[i].id] = the_ans;//记录答案
	}
    ...
    return 0;
}

例题

例题1 数列找不同

思路

莫队模板题,上面已经解释

代码

C++
#include <bits/stdc++.h>
using namespace std;

struct problem {int l,r,id;} problems[100005];
int n,T,a[100005],kind = 0,cnt[100005],block_siz;
bool ans[100005];

bool cmp(problem x,problem y)
{
	if(x.l / block_siz != y.l / block_siz) return x.l < y.l;
	if((x.l / block_siz)&1) return x.r < y.r;
	return x.r > y.r;
}

void add(int x)
{
	cnt[a[x]]++;
	if(cnt[a[x]] == 1) kind++;
	return;
}

void del(int x)
{
	cnt[a[x]]--;
	if(cnt[a[x]] == 0) kind--;
	return;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> T;
	block_siz = sqrt(n);
	for(int i = 1;i <= n;i++) cin >> a[i];
	for(int i = 1;i <= T;i++) cin >> problems[i].l >> problems[i].r;
	for(int i = 1;i <= T;i++) problems[i].id = i;
	sort(problems+1,problems+T+1,cmp);
	int l = problems[1].l,r = problems[1].l-1;
	for(int i = 1;i <= T;i++)
	{
		int pl = problems[i].l,pr = problems[i].r;
		while(pl < l) add(--l);
		while(r < pr) add(++r);
		while(l < pl) del(l++);
		while(pr < r) del(r--);
		ans[problems[i].id] = kind == r-l+1;
	}
	for(int i = 1;i <= T;i++)
	{
		if(ans[i]) cout << "Yes\n";
		else cout << "No\n";
	}
	return 0;
}

例题2 小B的询问

思路

模板,只是将记录答案的公式改一下。

代码

C++
#include <bits/stdc++.h>
using namespace std;

struct problem {int l,r,id;} problems[50005];
long long n,m,k,a[50005],block_siz,the_ans = 0,ans[50005],cnt[50005];

bool cmp(problem x,problem y)
{
	if(x.l/block_siz != y.l/block_siz) return x.l < y.l;
	if((y.l/block_siz)&1) return x.r < y.r;
	return x.r > y.r;
}

void add(int x)
{
	the_ans -= cnt[a[x]]*cnt[a[x]];
	cnt[a[x]]++;
	the_ans += cnt[a[x]]*cnt[a[x]];
	return;
}

void del(int x)
{
	the_ans -= cnt[a[x]]*cnt[a[x]];
	cnt[a[x]]--;
	the_ans += cnt[a[x]]*cnt[a[x]];
	return;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> k;
	block_siz = sqrt(n);
	for(int i = 1;i <= n;i++) cin >> a[i];
	for(int i = 1;i <= m;i++) cin >> problems[i].l >> problems[i].r;
	for(int i = 1;i <= m;i++) problems[i].id = i;
	sort(problems+1,problems+m+1,cmp);
	long long l = problems[1].l,r = problems[1].l-1;
	for(int i = 1;i <= m;i++)
	{
		int pl = problems[i].l,pr = problems[i].r;
		while(pl < l) add(--l);
		while(r < pr) add(++r);
		while(l < pl) del(l++);
		while(pr < r) del(r--);
		ans[problems[i].id] = the_ans;
	}
	for(int i = 1;i <= m;i++) cout << ans[i] << '\n';
	return 0;
}

例题3 [AHOI2013] 作业

思路

用值域树状数组统计每个数字的出现次数与是否出现即可。

代码

C++
#include <bits/stdc++.h>
using namespace std;

struct problem {int l,r,lv,rv,id;} problems[100005];
int n,m,a[100005],block_siz,ans1[100005],ans2[100005],tree[100005],cnt[100005],maxx = 0;

void change(int * tree,int x,int add_val)
{
	while(x <= maxx)
	{
		tree[x] += add_val;
		x += x&-x;//lowbit开函数会TLE一个点
	}
	return;
}

int get_sum(int * tree,int l,int r)
{
	int ans = 0;
	while(r)
	{
		ans += tree[r];
		r -= r&-r;
	}
	l--;
	while(l)
	{
		ans -= tree[l];
		l -= l&-l;
	}
	return ans;
}

bool cmp(problem x,problem y)
{
	if(x.l/block_siz != y.l/block_siz) return x.l < y.l;
	if((y.l/block_siz)&1) return x.r < y.r;
	return x.r > y.r;
}

void add(int x)
{
	change(cnt,a[x],1);
	if(get_sum(cnt,a[x],a[x]) == 1) change(tree,a[x],1);
	return;
}

void del(int x)
{
	change(cnt,a[x],-1);
	if(get_sum(cnt,a[x],a[x]) == 0) change(tree,a[x],-1);
	return;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	block_siz = sqrt(n);
	for(int i = 1;i <= n;i++) cin >> a[i];
	for(int i = 1;i <= n;i++) maxx = max(maxx,a[i]);
	for(int i = 1;i <= m;i++) cin >> problems[i].l >> problems[i].r >> problems[i].lv >> problems[i].rv;
	for(int i = 1;i <= m;i++) problems[i].id = i;
	stable_sort(problems+1,problems+m+1,cmp);
	int l = problems[1].l,r = problems[1].l-1;
	for(int i = 1;i <= m;i++)
	{
		int pl = problems[i].l,pr = problems[i].r;
		while(pl < l) add(--l);
		while(r < pr) add(++r);
		while(l < pl) del(l++);
		while(pr < r) del(r--);
		ans1[problems[i].id] = get_sum(cnt,problems[i].lv,min(maxx,problems[i].rv));//值域树状数组获取答案
		ans2[problems[i].id] = get_sum(tree,problems[i].lv,min(maxx,problems[i].rv));
	}
	for(int i = 1;i <= m;i++) cout << ans1[i] << ' ' << ans2[i] << '\n';
	return 0;
}
原文