算法总结——带修莫队

· · 算法·理论

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

带修莫队

思路与证明

带修莫队本质上就是在普通莫队的基础上增加了一个修改操作的维度。

在询问数据中加上一个修改时间维度,根据修改的数据,按需执行或回溯修改操作。

有三个维度,那么我们为三个分别分大小 n^{\frac{2}{3}} 的块,以均摊三维数据的震荡。在排序时,优先的两维分别按照所在块的编号排序,第三维直接排序。分别分析三维数据的处理耗时:

设序列长 n,有 b 个块。

左端点

跨块移动时,它们有序。时间复杂度为 O(n)。

块内移动时,它们无序。但受到两侧块的限制,每个块内的平均每次移动范围不超过 n^{\frac{2}{3}}。块的大小为 n^{\frac{1}{3}},有 n^{\frac{1}{3}} 个块,合并得块内移动时间复杂度为 O(n^{\frac{4}{3}})。

右端点

跨左端点块移动时,它们无序。每次跨块最多移动 n 次,共有 n^{\frac{1}{3}} 个块,合并得时间复杂度为 O(n^{\frac{4}{3}})。

在左端点块内跨右端点块移动时,它们有序,时间复杂度为 O(n)。

块内移动时,它们无序。但受到两侧块的限制,每个块内的平均每次移动范围不超过 n^{\frac{1}{3}}。块的大小为 n^{\frac{1}{3}},有 n^{\frac{2}{3}} 个块,合并得块内移动时间复杂度为 O(n^{\frac{4}{3}})。

修改序列

跨块移动时,它们无序。单次最多移动 n 次,有 n^{\frac{2}{3}} 个块,综合复杂度为 O(n^{\frac{5}{3}})。

块内移动时,它们有序。每个块内最多移动 n 次,有 n^{\frac{2}{3}} 个块,综合时间复杂度为 O(n^{\frac{5}{3}})。

合并所有时间复杂度,得总复杂度为 O(n^{\frac{5}{3}})。

代码

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

//cnt_p为询问个数,cnt_c为修改个数,
block_siz = pow(n,0.6666666666);
sort(problems+1,problems+cnt_p+1,cmp);
int l = problems[1].l,r = problems[1].l-1,tim = 0;
for(int i = 1;i <= cnt_p;i++)
{
    int pl = problems[i].l,pr = problems[i].r,ptim = problems[i].tim;
    while(pl < l) add(--l);
    while(r < pr) add(++r);
    while(l < pl) del(l++);
    while(pr < r) del(r--);
    while(ptim < tim) work_the_change(i,tim--);
    while(tim < ptim) work_the_change(i,++tim);
    //记录答案
}

例题

例题1 [国家集训队] 数颜色 / 维护队列

思路

模板题,用模板代码可通过。

代码

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

struct problem {int l,r,tim,id;} problems[133335];
struct chang {int x,val;} change_a[133335];
int n,m,a[133335],cnt_c = 0,cnt_p = 0,block_siz,ans[133335],the_ans = 0,cnt[1000005];

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

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

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

void work_the_change(int x,int tim)
{
    if(problems[x].l <= change_a[tim].x && change_a[tim].x <= problems[x].r)
    {
        del(change_a[tim].x);
        swap(change_a[tim].val,a[change_a[tim].x]);
        add(change_a[tim].x);
        swap(change_a[tim].val,a[change_a[tim].x]);
    }
    swap(change_a[tim].val,a[change_a[tim].x]);
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    block_siz = pow(n,0.6666666666);
    for(int i = 1;i <= n;i++) cin >> a[i];
    for(int i = 1;i <= m;i++)
    {
        char op;
        cin >> op;
        if(op == 'Q')
        {
            cnt_p++;
            cin >> problems[cnt_p].l >> problems[cnt_p].r;
            problems[cnt_p].id = cnt_p;
            problems[cnt_p].tim = cnt_c;
        }else if(op == 'R')
        {
            cnt_c++;
            cin >> change_a[cnt_c].x >> change_a[cnt_c].val;
        }
    }
    sort(problems+1,problems+cnt_p+1,cmp);
    int l = problems[1].l,r = problems[1].l-1,tim = 0;
    for(int i = 1;i <= cnt_p;i++)
    {
        int pl = problems[i].l,pr = problems[i].r,ptim = problems[i].tim;
        while(pl < l) add(--l);
        while(r < pr) add(++r);
        while(l < pl) del(l++);
        while(pr < r) del(r--);
        while(ptim < tim) work_the_change(i,tim--);
        while(tim < ptim) work_the_change(i,++tim);
        ans[problems[i].id] = the_ans;
    }
    for(int i = 1;i <= cnt_p;i++) cout << ans[i] << '\n';
    return 0;
}

例题2 Machine Learning

思路

用带修莫队模板处理 cnt,暴力处理 mex,因为 n 个元素每个元素出现次数得 mex 最多为 \sqrt{n}。

用树状数组也可以。

代码

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

struct problem {int l,r,tim,id;} problems[100005];
struct chang {int x,val;} change_a[100005];
int n,m,a[100005],cnt_c = 0,cnt_p = 0,block_siz,ans[100005],cnt[200005],buc[100005];
map<int,int>to;
set<int>vals;

void lisanhua(void)
{
    int rank = 0;
    for(auto val:vals) to[val] = ++rank;
    for(int i = 1;i <= n;i++) a[i] = to[a[i]];
    for(int i = 1;i <= cnt_c;i++) change_a[i].val = to[change_a[i].val];
    return;
}

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

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

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

void work_the_change(int x,int tim)
{
    if(problems[x].l <= change_a[tim].x && change_a[tim].x <= problems[x].r)
    {
        del(change_a[tim].x);
        swap(change_a[tim].val,a[change_a[tim].x]);
        add(change_a[tim].x);
        swap(change_a[tim].val,a[change_a[tim].x]);
    }
    swap(change_a[tim].val,a[change_a[tim].x]);
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    block_siz = pow(n,0.66);
    for(int i = 1;i <= n;i++) cin >> a[i];
    for(int i = 1;i <= n;i++) vals.insert(a[i]);
    for(int i = 1;i <= m;i++)
    {
        int op;
        cin >> op;
        if(op == 1)
        {
            cnt_p++;
            cin >> problems[cnt_p].l >> problems[cnt_p].r;
            problems[cnt_p].id = cnt_p;
            problems[cnt_p].tim = cnt_c;
        }else if(op == 2)
        {
            cnt_c++;
            cin >> change_a[cnt_c].x >> change_a[cnt_c].val;
            vals.insert(change_a[cnt_c].val);
        }
    }
    lisanhua();
    sort(problems+1,problems+cnt_p+1,cmp);
    int l = 1,r = 0,tim = 0;
    for(int i = 1;i <= cnt_p;i++)
    {
        int pl = problems[i].l,pr = problems[i].r,ptim = problems[i].tim;
        while(pl < l) add(--l);
        while(r < pr) add(++r);
        while(l < pl) del(l++);
        while(pr < r) del(r--);
        while(ptim < tim) work_the_change(i,tim--);
        while(tim < ptim) work_the_change(i,++tim);
        for(int j = 1;j <= n;j++)
        {
            if(buc[j] == 0)
            {
                ans[problems[i].id] = j;
                break;
            }
        }
    }
    for(int i = 1;i <= cnt_p;i++) cout << ans[i] << '\n';
    return 0;
}