算法总结——普通莫队2

· · 算法·理论

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

例题1 大爷的字符串题

思路

第一种情况造成 rp-1 是不可避免的,我们只能尽量使第二种情况造成的 rp-1 更少。注意到第二种情况的触发条件为 S 中有一个元素大于或等于插入的 x,所以我们要尽量使插入序列不出现一对相邻且前面的小于等于后面的数对,其实就是问把该区间排序后可以取出多少个严格上升子序列。每次肯定把户部巷扽的全取了,这样它的清空就是最少的,也就是众数出现的次数。

那么,该怎么快速求出众数出现的次数呢?可以用 cnt_i 记录再当前区间内元素 i 出现了几次,用 buc_i 记录 cnt_j 为 i 的合法 j 的数量。当区间扩张时,用当前的 cnt_{a_x} 尝试更新答案;当区间缩小时,若当对应数就是众数且没有其他众数,答案随该数减少。然后就可以用莫队做了。

代码

#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] 一个简单的询问

实际难度超过上一题。

思路

题目中有四个坐标,使用普通莫队的移动无法保证总移动数在 n\sqrt{n} 内,用两次莫队又无法公式中先乘再加的要求。题目的数据也不允许使用更高复杂的的算法了。

将公式变形:

\sum\limits_{x=0}^\infty \text{get}(l_1,r_1,x)\times \text{get}(l_2,r_2,x)

用前缀和思想拆分,得:

\sum\limits_{x=1}^n [\text{get}(1,r_1,x)-\text{get}(1,l_1-1,x)]\times [(\text{get}(1,r_2,x)-\text{get}(1,l_2-1,x)]

用分配律拆括号,得:

\sum\limits_{x=1}^n \text{get}(1,r_1,x)\times [(\text{get}(1,r_2,x)-\text{get}(1,l_2-1,x)]-\text{get}(1,l_1-1,x)\times [(\text{get}(1,r_2,x)-\text{get}(1,l_2-1,x)]

再次用分配律拆括号,得:

\sum\limits_{x=1}^n \text{get}(1,r_1,x)\times \text{get}(1,r_2,x)-\text{get}(1,r_1,x)\times\text{get}(1,l_2-1,x)-\text{get}(1,l_1-1,x)\times \text{get}(1,r_2,x)+\text{get}(1,l_1-1,x)\times\text{get}(1,l_2-1,x)

这个式子看起来很复杂,但可以发现,每个乘法都变成了两个坐标的算式。对于乘法的答案的区间扩展与缩小,可以利用乘法分配律将一参数加一,既 (x+1)\times y=xy+y,每次只需要加其对应的种类数。满足莫队的要求。具体看代码。

代码

#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;
}