算法总结——普通莫队2
program_xwl · · 算法·理论
莫队是一种分块思想衍生的离线算法,适合处理区间信息问题。
例题1 大爷的字符串题
思路
第一种情况造成
那么,该怎么快速求出众数出现的次数呢?可以用
代码
#include <bits/stdc++.h>
using namespace std;
struct lshnode
{
int val,id;
bool operator<(lshnode b) const {return val<b.val;}
} lsh[200005];
struct problem {int l,r,id;} problems[200005];
int n,m,a[200005],cnt[200005],buc[200005],the_ans = 0,ans[200005],block_siz;
void lisanhua(void)
{
for(int i = 1;i <= n;i++) lsh[i] = {a[i],i};
sort(lsh+1,lsh+n+1);
int rank = 0;
for(int i = 1;i <= n;i++)
{
if(lsh[i-1].val != lsh[i].val) rank++;
a[lsh[i].id] = rank;
}
return;
}
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)
{
buc[cnt[a[x]]]--;
cnt[a[x]]++;
the_ans = max(the_ans,cnt[a[x]]);
buc[cnt[a[x]]]++;
return;
}
void del(int x)
{
if(the_ans == cnt[a[x]] && buc[cnt[a[x]]] == 1) the_ans--;
buc[cnt[a[x]]]--;
cnt[a[x]]--;
buc[cnt[a[x]]]++;
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];
lisanhua();
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);
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--);
ans[problems[i].id] = -the_ans;
}
for(int i = 1;i <= m;i++) cout << ans[i] << '\n';
return 0;
}
例题2 [SNOI2017] 一个简单的询问
实际难度超过上一题。
思路
题目中有四个坐标,使用普通莫队的移动无法保证总移动数在
将公式变形:
用前缀和思想拆分,得:
用分配律拆括号,得:
再次用分配律拆括号,得:
这个式子看起来很复杂,但可以发现,每个乘法都变成了两个坐标的算式。对于乘法的答案的区间扩展与缩小,可以利用乘法分配律将一参数加一,既
代码
#include <bits/stdc++.h>
using namespace std;
struct problem {int r1,r2,id,mul;} problems[200005];//mul用于存储该询问作用到答案的是正还是负。
long long n,m,a[50005],block_siz,the_ans = 0,cnt1[50005],cnt2[50005],ans[50005];
bool cmp(problem x,problem y)
{
if(x.r1/block_siz != y.r1/block_siz) return x.r1 < y.r1;
if((x.r1/block_siz)&1) return x.r2 < y.r2;
return x.r2 > y.r2;
}
void add1(int x)
{
cnt1[a[x]]++;
the_ans += cnt2[a[x]];
return;
}
void add2(int x)
{
cnt2[a[x]]++;
the_ans += cnt1[a[x]];
return;
}
void del1(int x)
{
cnt1[a[x]]--;
the_ans -= cnt2[a[x]];
return;
}
void del2(int x)
{
cnt2[a[x]]--;
the_ans -= cnt1[a[x]];
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
cin >> m;
for(int i = 1;i <= m;i++)
{
int l1,r1,l2,r2;
cin >> l1 >> r1 >> l2 >> r2;
problems[i*4] = {r1,r2,i,1};//四个乘法式,上面已完成推导,注意它们都是累加到一个答案上的
problems[i*4-1] = {r1,l2-1,i,-1};
problems[i*4-2] = {l1-1,r2,i,-1};
problems[i*4-3] = {l1-1,l2-1,i,1};
}
block_siz = sqrt(n);
sort(problems+1,problems+m*4+1,cmp);
int r1 = 0,r2 = 0;//这里的两个指针指的是两个前缀的答案,而非区间的左右端点
for(int i = 1;i <= m*4;i++)
{
int pr1 = problems[i].r1,pr2 = problems[i].r2;
while(r1 < pr1) add1(++r1);
while(r2 < pr2) add2(++r2);
while(pr1 < r1) del1(r1--);
while(pr2 < r2) del2(r2--);
ans[problems[i].id] += the_ans*problems[i].mul;
}
for(long long i = 1;i <= m;i++) cout << ans[i] << '\n';
return 0;
}