带修莫队

· · 算法·理论

莫队,一定是不带修改的 \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ - 7月的芒果

~老师老师要是带修改怎么办呢?~

当然,~无解~,你可以用分块

~分块过不了怎么办~

~别做~

好了,切入

正题

带修莫队

初始化

在 q 数组里记录 (l,r,k),分别为每次查询的左边界,右边界与时间戳。

在 M 数组里记录 (pos,val),分别为每次修改操作的下标与值。

修改与查询

我们需要同时进行查询与修改操作。

先进行普通的查询操作。

接着,当当前时间 < q_i 的时间戳时,证明我们需要修改从当前时间到 q_i 的时间戳的操作。当当前时间 > q_i 的时间戳时,证明我们需要修改回来过多修改的操作。

修改

直接修改值,最后 swap 一下,方便下次修改回来。

void modify(int x, int t) {
    if (M[t].pos >= q[x].l && M[t].pos <= q[x].r) {
        SUB(a[M[t].pos]);
        ADD(M[t].val);
    }
    swap(a[M[t].pos], M[t].val);
}

例题

P1903 [国家集训队] 数颜色 / 维护队列

/*

writer:  Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:

*/
#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 1e6 + 10;
//const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int n, t, tot, a[N], b[N], cnt[N * 10], pos[N], ans[N]; 

struct Query {
    int l, r, id, t;
} q[N];

struct MODIFY {
    int pos, val;
} M[N];

void ADD(int x) {
    (++cnt[a[x]] == 1) && (tot++);
}

void SUB(int x) {
    (--cnt[a[x]] == 0) && (tot--);
}

bool cmp(Query a, Query b) {
    if (pos[a.l] != pos[b.l]) {
        return a.l < b.l;
    }
    if (pos[a.r] != pos[b.r]) {
        return a.r < b.r;
    }
    return a.t < b.t;
}

void modify(int x, int t) {
    if (M[t].pos >= q[x].l && M[t].pos <= q[x].r) {
        SUB(M[t].pos);
        int tmp = a[M[t].pos];
        a[M[t].pos] = M[t].val;
        ADD(M[t].pos);
        a[M[t].pos] = tmp;
    }
    swap(a[M[t].pos], M[t].val);
}
void work(){
    //please write your code here
    cin >> n >> t;
    int len = int(pow(n, 2.0 / 3.0));
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pos[i] = (i - 1) / len + 1;
    }
    int cnt_r = 0, cnt_q = 0;
    while (t--) {
        char c;
        cin >> c;
        if (c == 'Q') {
            ++cnt_q;
            cin >> q[cnt_q].l >> q[cnt_q].r;
            q[cnt_q].t = cnt_r;
            q[cnt_q].id = cnt_q;
        } else {
            ++cnt_r;
            cin >> M[cnt_r].pos >> M[cnt_r].val;
        }
    }
    sort (q + 1, q + 1 + cnt_q, cmp);
    int l = 1, r = 0, t = 0;
    for (int i = 1; i <= cnt_q; i++) {
        while (l < q[i].l)
            SUB(l++);
        while (l > q[i].l)
            ADD(--l);
        while (r < q[i].r)
            ADD(++r);
        while (r > q[i].r)
            SUB(r--);
        while (t < q[i].t)
            modify(i, ++t);
        while (t > q[i].t)
            modify(i, t--);
        ans[q[i].id] = tot;
    }
    for (int i = 1; i <= cnt_q; i++) {
        cout << ans[i] << '\n';
    }
}
problem T1(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1; while(t--){work();}
    return 0;
}

CF940F Machine Learning

数字过大,先离散化。

对于求出现次数的 mex,我们在修改完后暴力求就可以了。

/*

writer:  Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:

*/
#include<bits/stdc++.h>
#define ll long long
#define Code using
#define by namespace
#define cmy std
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
Code by cmy;
const int N = 1e6 + 10;
const int len = 2154;
//const int M = 100005;
const int INF = 1e9;
const int mod = 998244353;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
struct node{
    int p,x;
} c[100010];
int n, m, a[100010], b[200010], cnt[200010], tot[100010], qcnt, ccnt, qaq, now, ans[100010];
struct Query{
    int l, r, t, id;
    bool operator < (Query& y){
        return l / len == y.l / len ? (r / len == y.r / len ? t < y.t : r < y.r) : l < y.l;
    }
} q[100010];
void ADD(int x){
    --tot[cnt[x]];
    ++tot[++cnt[x]];
}
void SUB(int x){
    --tot[cnt[x]];
    ++tot[--cnt[x]];
}
void modify(int ti,int qu){
    if(c[ti].p >= q[qu].l && c[ti].p <= q[qu].r){
        SUB(a[c[ti].p]);
        ADD(c[ti].x);
    }
    swap(c[ti].x, a[c[ti].p]);
}
void work(){
    //please write your code here
    int l = 1, r = 0, op;
    n=read();
    m=read();
    for(int i = 1; i <= n; i++){
        b[qaq++]=a[i]=read();
    }
    for(int i = 0; i < m; i++){
        op = read();
        if(op == 1){
            q[++qcnt].l=read();
            q[qcnt].r=read();
            q[qcnt].t = ccnt;
            q[qcnt].id = qcnt;
        }else{
            c[++ccnt].p=read();
            b[qaq++]=c[ccnt].x=read();
        }
    }
    sort(b, b + qaq);
    qaq = unique(b,b + qaq) - b;
    for(int i = 1; i <= n; i++){
        a[i] = lower_bound(b, b + qaq, a[i]) - b;
    }
    for(int i = 1; i <= ccnt; i++){
        c[i].x = lower_bound(b, b + qaq, c[i].x) - b;
    }
    sort(q + 1,q + qcnt + 1);
    for(int i = 1; i <= qcnt; i++){
        while(l > q[i].l){
            ADD(a[--l]);
        }
        while(r < q[i].r){
            ADD(a[++r]);
        }
        while(l < q[i].l) {
            SUB(a[l++]);
        }
        while(r > q[i].r){
            SUB(a[r--]);
        }
        while(now < q[i].t){
            modify(++now,i);
        }
        while(now > q[i].t){
            modify(now--,i);
        }
        for(ans[q[i].id] = 1; tot[ans[q[i].id]] > 0; ++ans[q[i].id]);
    }
    for(int i = 1; i <= qcnt; i++){
        cout << ans[i] << "\n";
    }
}
problem T1() {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--) {
        work();
    }
    return 0;
}