P6018 [Ynoi2010] Fusion tree 題解

Athanasy發表於2024-04-19

題目連結:Fusion tree

大部分人貌似用的邊權 01 Trie,實際這題用點權 01 Trie 類似文藝平衡樹去寫更方便。

考慮兩種常見的區間維護:

  1. 線段樹。使用的是父節點資訊是歸併了左右區間的資訊,適用於不需要考慮父節點的貢獻的資訊。

  2. 文藝平衡樹。每個點就是一個資訊,歸併左右子樹,外加當前自身節點的資訊歸併。

0-1Trie 在平衡樹板題當中倒是有很多人拿來去代替。這裡講講本題怎麼用第二種方式去寫。

0-1 Trie 最基本的資訊:當前所在一層為某一位,即某個節點最基本的資訊為某一位的資訊,然後這一位可以拆成 "0/1",同時這一位是 \(0\) 或者是 \(1\) 的資訊。

對於樹類維護資訊思考:從單點基本資訊 \(\Rightarrow\) 歸併後的資訊。

思考本題大致需要維護什麼資訊?

基本的每一位的 \(0\) 或者 \(1\) 的數量,再考慮本題詢問異或和,那顯然我們需要知道整棵樹所存的樹的異或和,這東西就是我們需要 "歸併" 得到的。考慮如何由左右子樹 \(+\) 當前這一位去歸併得到。

異或和和加法這一類問題是差不多的,可加可減。

考慮:

\(1\rightarrow 1\times 10+2 \rightarrow 12 \times 10 +3 \rightarrow 123\times 10 +4 \rightarrow 1234\)

加法其實可以這麼去累計每一位的貢獻。那麼我們來考慮異或和是否也能這樣得到。

對於一個數而言:

\( 1<<1 \oplus 2 \rightarrow 12<<1 \oplus 3 \rightarrow 123<<1 \oplus 4 \rightarrow 1234 \)

一樣是從高位到低位,我們樹類結構顯然是從葉子往上歸併,那麼這個 0-1 Trie 顯然應該由低位到高位的去建,讓葉子節點為高位資訊,這樣才能類似上述過程父節點歸併資訊。這和常用的 0-1 Trie 略帶區別。

現在不是一個數了,而是若干個數,還是考慮相鄰兩位的合併:

  1. 已經知道左右子樹的異或和,顯然它們的異或結果即為高位異或和,按上述描述需要右移一位。

  2. 還知道當前這一位的數量,要合併資訊,則需要考慮當前這一位是否有貢獻,那麼我們從最開始的定義來看。首先它一定要屬於 \(1\) 的貢獻,因為對於每一位來說都有對應的一層表示,對應的一層則有兩個樹節點,一個表示是 \(0\),一個表示是 \(1\) 的貢獻。那麼是 \(0\) 的數這一位異或結果無論如何都是 \(0\)。考慮如果是 \(1\) 的數則需要考慮數量,如果是奇數則有貢獻,否則偶數個 \(1\) 異或結果是 \(0\)

到此,你會發現還需要維護一個資訊:\(type\),表示當前節點的型別,是 \(0\) 還是 \(1\),和析合樹的析點與合點型別定義方式一致。這樣一來,這個歸併資訊程式碼就出來了:

參照程式碼
xorSum(curr) = (xorSum(left(curr)) ^ xorSum(right(curr))) << 1 ^ cnt(curr) & type(curr) & 1;

把左右子樹異或和移動一位,當前位則為低一位,他是否貢獻,由數量和型別共同決定。

現在難點來了,對於 0-1 Trie 來說,是具有空根節點的概念的,即根節點並不表示任何一位,而是歸併左右節點資訊,這玩意不就和線段樹一樣嗎?所有需要特判。

回到本題

考慮本題怎麼做。觀察到它的問法很特別,維護一個點的 \(1\) 鄰域的若干個點。無根樹不好做,我們變為有根樹,就確定 \(1\) 為根。考慮每個點的鄰域的點,分為兒子和一個父親,考慮 0-1 Trie去維護,考慮如果父親加入這個樹,那麼當父親改變時,實際上會影響下方包含它的樹,與上方包含它的樹,那麼考慮到這玩意是單點維護,那麼這個點的改變就不會影響它的所有兒子的樹了,否則它改變時,下方包含的 Trie 樹全都得改變,難以接受,所以父親單獨維護時,只需要考慮上方包含它的 Trie樹,顯然就一個。

看圖就能理解我說的意思,所以 \(fa\) 單獨維護,它的變化就只會影響 \(fa[fa]\) 的樹。

考慮第一個操作,全域性 \(+1\),先考慮 0-1 Trie 咋做這玩意?

搬一張 oiwike 的圖看看,其實很簡單,從低位到高位找到第一個 \(0\),然後設做 \(1\),它低位的 \(1\) 則變成 \(0\) 即可。但這是一個數的,若干個數同時做,其實不就是類似 \(0-1\) 線段樹內的所有 \(0 \rightarrow 1\) 操作嗎?而所有的 \(1\) 則變成 \(0\),所以本質就是對偶問題的交換答案,只需要交換兩棵樹即可。如何再暴力修改父親的父親樹,刪除父親節點的值,然後 \(+1\) 後再插入回去。

注意交換細節,交換後 \(type\) 需要注意重新賦值了,所以導致原來的資訊需要重新 pushUp,注意到這一點就沒問題了。

操作三就是查詢,先不考慮操作二,操作三查詢顯然是鄰域查詢異或上父親的值就行了。現在考慮操作一對操作三的影響,顯然那個鄰域兒子異或值沒啥影響,主要是父親的貢獻,我們發現操作一會使得對所有的 \(son\) 都有 \(+1\) 操作,這個 \(+1\) 顯然不能暴力,那麼就類似標記永久化,打 lazy,\(lazy[curr]\) 表示 \(curr\) 的所有節點 \(+\) 多少。這樣一來就能很快拿到一個點的準確值為:\(val[curr]+lazy[fa[curr]]\)

這樣一來一三操作解決了。二操作就是一棵 \(0-1 Trie\) 上刪除原來的這個數,再加上新的這個數,考慮一下影響只有它父親的 0-1 Trie 樹。至此,都解決了。

參照程式碼
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用於Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c) if (b & 1) (ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-') sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char)) return;
    if (x < 0) x = -x, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow) return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3()
    {
        one = tow = three = 0;
    }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y) x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y) x = y;
}

constexpr int N = 5e5 + 10;
constexpr int T = 20; //最高位
int n, m;
int a[N], fa[N], lazy[N];
vector<int> child[N];

struct Trie
{
    int xorSum;
    int cnt;
    int child[2];
    int type; //型別 0/1
} node[N * T];

#define xorSum(x) node[x].xorSum
#define cnt(x) node[x].cnt
#define type(x) node[x].type
#define child(x,y) node[x].child[y]
#define left(x) child(x,0)
#define right(x) child(x,1)
int root[N], cnt;

//歸併資訊,idx=0則為根節點,不表示任何一位
inline void pushUp(const int curr, const int idx)
{
    const int v = idx != 0; //是否是根節點,非根節點則為 ans<<1^(當前點的貢獻)
    xorSum(curr) = (xorSum(left(curr)) ^ xorSum(right(curr))) << v ^ v & type(curr) & cnt(curr);
}

inline void update(int& curr, const int val, const int diff, const int type = 0, const int idx = 0)
{
    if (idx > T) return;
    if (!curr) curr = ++cnt;
    type(curr) = type;
    cnt(curr) += diff;
    update(child(curr, val&1), val >> 1, diff, val & 1, idx + 1);
    pushUp(curr, idx);
}

//全域性+1
inline void addOne(const int curr, const int idx = 0)
{
    swap(left(curr),right(curr));
    type(left(curr)) = 0, type(right(curr)) = 1; //注意重新賦值型別
    if (left(curr)) addOne(left(curr), idx + 1);
    //注意左右子樹的答案需要重新計算,因為型別變了
    pushUp(left(curr), idx + 1);
    pushUp(right(curr), idx + 1);
    pushUp(curr, idx);
}

//真實值
inline int getVal(const int curr)
{
    return a[curr] + lazy[fa[curr]];
}

//操作一
inline void op1(const int curr)
{
    addOne(root[curr]); //兒子全域性+1
    const int old = getVal(fa[curr]);
    //父親的影響
    if (fa[fa[curr]])
    {
        update(root[fa[fa[curr]]], old, -1);
        update(root[fa[fa[curr]]], old + 1, 1);
    }
    if (fa[curr]) a[fa[curr]]++;
    lazy[curr]++;
}

//操作二,暴力刪除,暴力新增
inline void op2(const int curr, const int val)
{
    if (fa[curr]) update(root[fa[curr]], getVal(curr), -1);
    a[curr] -= val;
    if (fa[curr]) update(root[fa[curr]], getVal(curr), 1);
}

//操作三,注意下如果只有一個點,沒有鄰域返回0
inline int op3(const int curr)
{
    return xorSum(root[curr]) ^ getVal(fa[curr]);
}

//為所有兒子節點建樹
inline void dfs(const int curr, const int pa)
{
    fa[curr] = pa;
    for (const int nxt : child[curr])
    {
        if (nxt == pa) continue;
        update(root[curr], a[nxt], 1);
        dfs(nxt, curr);
    }
}


inline void solve()
{
    cin >> n >> m;
    forn(i, 1, n-1)
    {
        int u, v;
        cin >> u >> v;
        child[u].push_back(v);
        child[v].push_back(u);
    }
    forn(i, 1, n) cin >> a[i];
    dfs(1, 0);
    while (m--)
    {
        int op, curr, val;
        cin >> op >> curr;
        if (op == 1) op1(curr);
        else if (op == 2) cin >> val, op2(curr, val);
        else cout << op3(curr) << endl;
    }
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test) solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\(op1/op2\) 單次操作 \(O(\log{V})\)\(op3\) 則是 \(O(1)\)

\[最壞時間複雜度為:\ O((n+m)\log{V}) \]

相關文章