跳到正文

个人记录

莫队学习笔记

原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月19日
篇幅
约 11 分钟

版本

  1. v1当前

目前只会普通莫队 qwq,带修莫队只会一点,回滚莫队只会板子,树上莫队不是很会。

莫队这玩意操作是很简单的,基本的思想是将查询离线下来按照一定的次序进行排序然后逐个进行处理,并且通过数列分块的技巧将时间复杂度巧妙的降到 O(nn)O(n \sqrt n) 左右。

对于基本的莫队,一般可以处理求区间不同种类的数的个数、区间众数的出现次数、区间 mex\text{mex} 这些问题,在面对一些问题的时候巧妙的将问题转化为上面莫队可以处理的问题便可以使用莫队解决。

注意很多莫队的题目要求我们对离散化、STL 这一类工具和技巧比较熟悉,否则未必能够成功转化。

接下来就是例题了:

P3901 数列找不同

现有数列 A1,A2,…,ANA_1,A_2,\ldots,A_N,QQ 个询问 (Li,Ri)(L_i,R_i),询问 ALi,ALi+1,…,ARiA_{L_i} ,A_{L_i+1},\ldots,A_{R_i} 是否互不相同。

对每个询问输出一行,Yes 或 No。

对于 100%100\% 的数据,1≤N,Q≤1051 \le N,Q \le 10^5,1≤Ai≤N1 \le A_i \le N,1≤Li≤Ri≤N1 \le L_i \le R_i \le N。

解题思路: 考虑把区间每个数互不相同转化成区间不同种类的数的个数为 Ri−Li+1R_i - L_i + 1。所以问题就变为求区间不同种类的数的个数,然后进行判断,可以使用莫队进行解决。

不过这里提一嘴,这个题目不仅可以使用莫队解决,同样是离线,参考类似 HH 的项链这个题目的树状数组做法进行微调也是可以通过这个题目的。

code :

C++
#include <bits/stdc++.h>
#define ll long long
#define db double
#define vec vector
#define pll pair<ll, ll>
#define mkp make_pair
#define endl "\n"

using namespace std;

const ll mod = 998244353;

namespace fastio {
    char buf[1 << 21], *p1 = buf, *p2 = buf;

    const ll getc() {
        return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
    }

    const ll read() {
        ll x = 0, f = 1;

        char ch = getc();

        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1; ch = getc();
        }

        while (ch >= '0' && ch <= '9') {
            x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
        }

        return x * f;
    }

    const void write(ll x) {
        if (x < 0) {
            putchar('-'), x = -x;
        }

        ll sta[35], top = 0;

        do {
            sta[top++] = x % 10, x /= 10;
        } while (x);

        while (top) putchar(sta[--top] + 48);
    }
}

#define rd fastio::read
#define wt fastio::write
#define gc fastio::getc

ll n, m, q, bl, a[200005], cnt[200005], ask[200005]; string s;

ll opt, l, r, ans = 0;

struct query {
    ll l, r, id;

    friend bool operator < (query x, query y) {
        ll xb = x.l / bl, yb = y.l / bl;

        if (xb == yb) {
            return x.r < y.r;
        }

        return xb < yb;
    }
} qry[200005] ;

void add(ll x) {
    ans += (cnt[a[x]] == 0), cnt[a[x]] ++;
}

void del(ll x) {
    ans -= (cnt[a[x]] == 1), cnt[a[x]] --;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    ll i, j, k, x, y, z, res = 0, now;

    n = rd(), m = rd(), bl = sqrt(n);

    for (i = 1; i <= n; i++) a[i] = rd();

    for (i = 1; i <= m; i++) qry[i].l = rd(), qry[i].r = rd(), qry[i].id = i;

    sort(qry + 1, qry + m + 1);

    ll l = 1, r = 0;

    for (i = 1; i <= m; i++) {
        while (l > qry[i].l) add(--l);
        while (r < qry[i].r) add(++r);
        while (l < qry[i].l) del(l++);
        while (r > qry[i].r) del(r--);

        ask[qry[i].id] = (ans == qry[i].r - qry[i].l + 1);
    }

    for (i = 1; i <= m; i++) puts(ask[i] ? "Yes" : "No");

    return 0;
}

P2709 小B的询问

小B 有一个长为 nn 的整数序列 aa,值域为 [1,k][1,k]。 他一共有 mm 个询问,每个询问给定一个区间 [l,r][l,r],求:

∑i=1kci2\sum\limits_{i=1}^k c_i^2

其中 cic_i 表示数字 ii 在 [l,r][l,r] 中的出现次数。 小B请你帮助他回答询问。

对于 100%100\% 的数据,1≤n,m,k≤5×1041\le n,m,k \le 5\times 10^4。

解题思路: 题目已经说了就是要知道区间每个数出现次数的平方和。那么对于莫队的 add 和 del 函数,考虑 ci2∼(ci+1)2c_i ^ 2 \sim (c_i + 1) ^ 2 所带来的增量和 ci2∼(ci−1)2c_i ^ 2 \sim (c_i - 1) ^ 2 所带来的减量。即:

(ci+1)2=ci2+2ci+1,(ci+1)2−ci2=2ci+1(ci−1)2=ci2−2ci+1,(ci−1)2−ci2=2ci−1(c_i + 1) ^ 2 = c_i ^ 2 + 2c_i + 1, (c_i + 1) ^ 2 - c_i ^ 2 = 2c_i + 1 \\ (c_i - 1) ^ 2 = c_i ^ 2 - 2c_i + 1, (c_i - 1) ^ 2 - c_i ^ 2 = 2c_i - 1

最后用一下莫队,做完了。

code :

C++
#include <bits/stdc++.h>
#define ll long long
#define db double
#define vec vector
#define pll pair<ll, ll>
#define mkp make_pair
#define endl "\n"

using namespace std;

const ll mod = 998244353;

namespace fastio {
    char buf[1 << 21], *p1 = buf, *p2 = buf;

    const ll getc() {
        return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
    }

    const ll read() {
        ll x = 0, f = 1;

        char ch = getc();

        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1; ch = getc();
        }

        while (ch >= '0' && ch <= '9') {
            x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
        }

        return x * f;
    }

    const void write(ll x) {
        if (x < 0) {
            putchar('-'), x = -x;
        }

        ll sta[35], top = 0;

        do {
            sta[top++] = x % 10, x /= 10;
        } while (x);

        while (top) putchar(sta[--top] + 48);
    }
}

#define rd fastio::read
#define wt fastio::write
#define gc fastio::getc

ll n, m, q, bl, a[200005], cnt[200005], ask[200005]; string s;

ll opt, l, r, ans = 0;

struct query {
    ll l, r, id;

    friend bool operator < (query x, query y) {
        ll xb = x.l / bl, yb = y.l / bl;

        if (xb == yb) {
            return x.r < y.r;
        }

        return xb < yb;
    }
} qry[200005] ;

void add(ll x) {
    ans += (2 * cnt[a[x]] + 1), cnt[a[x]] ++;
}

void del(ll x) {
    ans -= (2 * cnt[a[x]] - 1), cnt[a[x]] --;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    ll i, j, k, x, y, z, res = 0, now;

    n = rd(), m = rd(), k = rd(), bl = sqrt(n);

    for (i = 1; i <= n; i++) a[i] = rd();

    for (i = 1; i <= m; i++) qry[i].l = rd(), qry[i].r = rd(), qry[i].id = i;

    sort(qry + 1, qry + m + 1);

    ll l = 1, r = 0;

    for (i = 1; i <= m; i++) {
        while (l > qry[i].l) add(--l);
        while (r < qry[i].r) add(++r);
        while (l < qry[i].l) del(l++);
        while (r > qry[i].r) del(r--);

        ask[qry[i].id] = ans;
    }

    for (i = 1; i <= m; i++) wt(ask[i]), puts("");

    return 0;
}
原文