跳到正文

题解

如何击败回滚莫队

mian_ku约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年10月14日
收录
2026年10月14日
更新
2026年11月9日
篇幅
约 12 分钟
题目
P9340

版本

  1. v1当前

主播主播,我不会回滚莫队,我只会老爷又老爷的普通莫队怎么办?

没关系兄弟,虽然老爷但是够轮椅。考虑用 6464 叉树维护前驱和后继。面对一颗节点,如何找到它的后继?发现可以用一个 unsigned long long 维护当前存在数的状态,然后你发现用位运算操作可以在 O(1)O(1) 时间内找到前驱 / 后继,树高居然是 O(log⁡ωn)O(\log_\omega n) 的,在本题它居然不超过 33!然后直接上寻宝游戏求虚树大小就可以轻松莫队了。时间复杂度 O(nnlog⁡ωn)O(n\sqrt n \log_\omega n),但是压根不用卡常,没有 I/O 优化还是能轻松跑过。

请输入文本

如果你懒不妨开 nω\frac{n}{\omega} 个 bitset 直接查前驱后继,容易发现这个最劣要查询 2525 次,非常不优秀,但是通过了本题(笑)。

请输入文本

跑得还比这个快。

C++
#include <bits/stdc++.h>
//#define int long long
using namespace std;

#define pii pair <int, int>
#define fi first 
#define se second

const int N = 1e5, LOG = 19;
using ull = unsigned long long;
struct Tree64 {
    struct bitcol {
        ull x;
        bitcol (ull _x = 0) { x = _x; } 
        inline void set (int p) { x |= (1ull << p); }
        inline void drop (int p) { x &= ~(1ull << p); }
        inline int get (int p) { return (x >> p) & 1; }
        inline ull get (int l, int r) { return (x >> l) & ((1ull << r - l) - 1); }
        inline bool empty () { return x == 0; } 
        inline int next (int p) {
            if (p > 63) return -1; 
            ull t = x & (~0ull << p);
            if (t) return __builtin_ctzll (t);
            return -1;   
        }
        inline int prev (int p) { 
            if (p < 0) return -1;
            ull t = (p == 63) ? x : (x & ((1ull << p + 1) - 1));
            if (t) return 63 - __builtin_clzll (t);
            return -1;
        }
        inline int first () { return next (0); }
        inline int last () { return prev (63); }
    };
    bitcol a, b [64], c [64] [64];
    void insert (bitcol p) {
        ull i1 = p. get (12, 18), i2 = p. get (6, 12), i3 = p. get (0, 6);
        a. set (i1); b [i1]. set (i2); c [i1] [i2]. set (i3);
    }
    void erase (bitcol p) {
        ull i1 = p. get (12, 18), i2 = p. get (6, 12), i3 = p. get (0, 6);
        c [i1] [i2]. drop (i3);
        if (c [i1] [i2]. empty ()) b [i1]. drop (i2);
        if (b [i1]. empty ()) a. drop (i1);
    }
    int find_next (bitcol p) { 
        ull i1 = p. get (12, 18), i2 = p. get (6, 12), i3 = p. get (0, 6);
        int r3 = c [i1] [i2]. next (i3), r2 = b [i1]. next (i2 + 1), r1 = a. next (i1 + 1);
        if (r3 + 1) return i1 * 4096 + i2 * 64 + r3;
        if (r2 + 1) return i1 * 4096 + r2 * 64 + c [i1] [r2]. first ();
        if (r1 + 1) return r1 * 4096 + b [r1]. first () * 64 + c [r1] [b [r1]. first ()]. first ();
        return -1;
    }
    int find_prev (bitcol p) {
        ull i1 = p. get (12, 18), i2 = p. get (6, 12), i3 = p. get (0, 6);
        int r3 = c [i1] [i2]. prev (i3), r2 = b [i1]. prev (i2 - 1), r1 = a. prev (i1 - 1);
        if (r3 + 1) return i1 * 4096 + i2 * 64 + r3;
        if (r2 + 1) return i1 * 4096 + r2 * 64 + c [i1] [r2]. last ();
        if (r1 + 1) return r1 * 4096 + b [r1]. last () * 64 + c [r1] [b [r1]. last ()]. last ();
        return -1;
    }
};
using Que = tuple <int, int, int>;
int n, m, q, new_id, cnt, dep [N + 5], eul [N + 5], b, a [N + 5],
    ans [N + 5], sum, tot, dfn [N + 5], idfn [N + 5], t [N + 5];
pii st [LOG] [N * 2 + 5];
Que que [N + 5];
vector <int> G [N + 5];
Tree64 s;
bool cmp (Que x, Que y) {
    auto [lx, rx, idx] = x;
    auto [ly, ry, idy] = y;
    if (lx / b == ly / b)
        if (rx == ry) return 0; 
        else return ((lx / b) & 1) ^ (rx < ry);
    return lx < ly;
}
void dfs (int u, int fa) {
    dep [u] = dep [fa] + 1; 
    st [0] [++ new_id] = {dep [u], u};
    eul [u] = new_id; dfn [u] = ++ tot; idfn [tot] = u;
    for (auto to : G [u]) if (to ^ fa) {
        dfs (to, u);
        st [0] [++ new_id] = {dep [u], u};
    }
}
int lca (int x, int y) {
    x = eul [x]; y = eul [y];
    if (x > y) swap (x, y);
    int l = __lg (y - x + 1);
    return min (st [l] [x], st [l] [y - (1 << l) + 1]). se;
}
int dist (int x, int y) { return dep [x] + dep [y] - 2 * dep [lca (x, y)]; }
void add (int i) {
    t [a [i]] ++;
    if (t [a [i]] > 1) return ;
    if (cnt == 1) sum += 2 * dist (idfn [s. find_next (0)], a [i]);
    else if (cnt > 1) {
        int pre = s. find_prev (dfn [a [i]]); 
        if (pre == -1) pre = s. find_prev (n);
        int nxt = s. find_next (dfn [a [i]]);
        if (nxt == -1) nxt = s. find_next (1);
        sum -= dist (idfn [pre], idfn [nxt]);
        sum += (dist (idfn [pre], a [i]) + dist (a [i], idfn [nxt]));
    }
    s. insert (dfn [a [i]]); cnt ++;
} 
void del (int i) {
    t [a [i]] --;
    if (t [a [i]] > 0) return ;
    s. erase (dfn [a [i]]); cnt --;
    if (cnt == 1) sum -= 2 * dist (idfn [s. find_next (0)], a [i]);
    else if (cnt > 1) {
        int pre = s. find_prev (dfn [a [i]]); 
        if (pre == -1) pre = s. find_prev (n);
        int nxt = s. find_next (dfn [a [i]]);
        if (nxt == -1) nxt = s. find_next (1);
        sum += dist (idfn [pre], idfn [nxt]);
        sum -= (dist (idfn [pre], a [i]) + dist (a [i], idfn [nxt]));
    }
}
void Turn () {
    cin >> n >> m >> q; b = ceil (sqrt (m));
    for (int i = 1 ; i < n ; i ++) {
        int a, b; cin >> a >> b;
        G [a]. push_back (b);
        G [b]. push_back (a);
    } dfs (1, 0);
    for (int j = 1 ; j < LOG ; j ++)
        for (int i = 1 ; i + (1 << j) - 1 <= new_id ; i ++)
            st [j] [i] = min (st [j - 1] [i], st [j - 1] [i + (1 << j - 1)]);
    for (int i = 1 ; i <= m ; i ++) cin >> a [i];
    for (int i = 1 ; i <= q ; i ++) {
        int l, r; cin >> l >> r;
        que [i] = {l, r, i};
    }
    sort (que + 1, que + q + 1, cmp);
    int x = 1, y = 0;
    for (int i = 1 ; i <= q ; i ++) {
        auto [l, r, id] = que [i];
        while (l < x) add (-- x);
        while (y < r) add (++ y);
        while (x < l) del (x ++);
        while (r < y) del (y --);
        ans [id] = sum / 2 + 1;
    }
    for (int i = 1 ; i <= q ; i ++)
        cout << ans [i] << endl;
}

int _; void Init () {
	ios :: sync_with_stdio (0); cin. tie (0); cout. tie (0);
    _ = 1;
//	freopen (".in", "r", stdin);
//	freopen (".out", "w", stdout);
//	cin >> _;
}

    signed main () { Init ();
    while (_ --> 0)
    Turn (); return 0; }
``
原文
存档评论5 条抓取于