P10604 BZOJ4317 Atm 的树 题解

题目链接:P10604 BZOJ4317 Atm 的树

简单点分树题。我们考虑第 \(k\) 大问题常见的两种做法:

  1. 树上二分。

  2. 二分 \(+\) check。

显然这题由于有若干个点对应的路径关系,并不太好用前者,我们不可能为某个点经过的所有路径都建立一棵权值线段树,然后再权值线段树上二分,因为路径总数是 \(O(n^2)\) 的。

考虑第二种,我们二分 \(k\) 路径长:

我们对于大型路径问题常见的方式就是 点分治 或者 点分树 来做。这题由于我们使用点分树来做:

对于点分树来说:

  1. 某个分治中心将会在它的后代分治中心消失,所以包含该分治中心的点的时间仅仅只有从该点到所有祖先分治中心节点的每个分治中心所在的时间树,树高为 \(O(\log{n})\),暴力访问即为 \(O(\log{n})\)。本质上分治中心可以看做该树在某一时间下的时间树,特征为以该分治中心作为根,其祖先分治中心方向的子树全部消失

  2. 对于每个分治中心的贡献路径,当且仅当该路径包含该分治中心,我们常见的把路径分为两类:
    (1) 以该分治中心作为端点进行衍生。
    (2) 以该分治中心作为分割点,从任意两棵不相同的子树中任取两个点作为端点,拼接而成的路径。

  3. 枚举祖先分治中心,基于点 \(2\) ,考虑同时经过当前点和该分治中心的点的路径即为该分治中心对当前这个点作为分治中心的贡献。

常见的点分树维护:

  1. 维护以某个点作为根即分治中心的所有链信息。

  2. 维护某个分治中心的父分治中心到该分治中心方向上的链信息。

通过 \(1\) 去掉 \(2\),即为除了当前子树以外的其他子树的链信息。

回到本题

考虑我们即为寻找所有经过当前查询点的路径中,\(\le mid\) 的数目,判断是否 \(\ge k\) 即为 \(check\)。那么最好的方式显然可以考虑随便维护一个权值数据结构,这边直接使用了:动态开点树状数组

考虑查询,对于一个 \(fa\) 与当前点的贡献,显然我们可以使用树上前缀和处理出二者之间的路径 \(d\),然后查询 \(\le mid-d\) 的数目,即可查询到该分治中心的贡献,这个可以用上述点分树维护的容斥思路算出,注意 \(d\) 也是一个答案,当然你可以可以考虑维护 \(0\) 这条空链,但要注意去掉,我自然是暴力判断 \(d\) 了。二分的话,上界为 \(wn\),而 \(w\) 又比较小,所以这样一来单点查询的复杂度显然就是 \(O(\log^3{n})\)

参照代码
#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;
}

struct Hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
        x = (x ^ x >> 27) * 0x94d049bb133111eb;
        return x ^ x >> 31;
    }

    static size_t get(const uint64_t x)
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }

    template <typename T>
    size_t operator()(T x) const
    {
        return get(std::hash<T>()(x));
    }

    template <typename F, typename S>
    size_t operator()(pair<F, S> p) const
    {
        return get(std::hash<F>()(p.first)) ^ std::hash<S>()(p.second);
    }
};

constexpr int N = 1e5 + 10;
constexpr int T = log2(N) + 1;
constexpr int MX = 150000;
vector<pii> child[N];
int n, k;

struct
{
    int fa[N][T + 1], deep[N], pre[N];

    void dfs(const int curr, const int pa)
    {
        deep[curr] = deep[fa[curr][0] = pa] + 1;
        forn(i, 1, T) fa[curr][i] = fa[fa[curr][i - 1]][i - 1];
        for (const auto [nxt,val] : child[curr])
        {
            if (nxt != pa) pre[nxt] = pre[curr] + val, dfs(nxt, curr);
        }
    }

    int LCA(int x, int y) const
    {
        if (deep[x] < deep[y]) swap(x, y);
        forv(i, T, 0) if (deep[fa[x][i]] >= deep[y]) x = fa[x][i];
        if (x == y) return x;
        forv(i, T, 0) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
        return fa[x][0];
    }

    int dist(const int x, const int y) const
    {
        return pre[x] + pre[y] - 2 * pre[LCA(x, y)];
    }
} LCA;

struct
{
    hash2<int, int, Hash> bit[N];

    void add(const int root, int x)
    {
        while (x <= MX) bit[root][x]++, x += lowBit(x);
    }

    int query(const int root, int x)
    {
        int ans = 0;
        while (x)
        {
            if (bit[root].find(x) != bit[root].end()) ans += bit[root][x];
            x -= lowBit(x);
        }
        return ans;
    }
} cnt, cntFa;

struct
{
    bool del[N];
    int sumSize, maxSon, root;
    int siz[N], dist[N][T + 1], fa[N], deep[N];

    void makeRoot(const int curr, const int pa)
    {
        siz[curr] = 1;
        int currSize = 0;
        for (const auto nxt : child[curr] | views::keys)
        {
            if (del[nxt] or nxt == pa) continue;
            makeRoot(nxt, curr);
            siz[curr] += siz[nxt];
            uMax(currSize, siz[nxt]);
        }
        uMax(currSize, sumSize - siz[curr]);
        if (currSize < maxSon) maxSon = currSize, root = curr;
    }

    void dfs(const int curr, const int pa)
    {
        siz[curr] = 1;
        for (const int nxt : child[curr] | views::keys)
        {
            if (!del[nxt] and nxt != pa) dfs(nxt, curr), siz[curr] += siz[nxt];
        }
    }

    void buildCurr(const int curr, const int v, const int rt, const int pa)
    {
        for (const auto [nxt,val] : child[curr])
        {
            if (del[nxt] or nxt == pa) continue;
            cnt.add(rt, v + val);
            buildCurr(nxt, v + val, rt, curr);
        }
    }

    void buildFa(const int curr, const int v, const int rt, const int pa)
    {
        cntFa.add(rt, v);
        for (const auto [nxt,val] : child[curr])
        {
            if (del[nxt] or nxt == pa) continue;
            buildFa(nxt, v + val, rt, curr);
        }
    }

    void build(const int curr)
    {
        del[curr] = true;
        buildCurr(curr, 0, curr, 0);
        for (const auto [nxt,val] : child[curr])
        {
            if (del[nxt]) continue;
            sumSize = maxSon = siz[nxt];
            makeRoot(nxt, 0);
            dfs(root, 0);
            buildFa(nxt, val, root, curr);
            deep[root] = deep[fa[root] = curr] + 1;
            build(root);
        }
    }

    void init()
    {
        LCA.dfs(1, 0);
        sumSize = maxSon = n;
        makeRoot(1, 0);
        build(root);
        forn(son, 1, n)
        {
            for (int rt = fa[son]; rt; rt = fa[rt]) dist[son][deep[son] - deep[rt]] = LCA.dist(son, rt);
        }
    }

    bool check(const int curr, const int len) const
    {
        int ans = cnt.query(curr, len);
        for (int nxt = curr; fa[nxt]; nxt = fa[nxt])
        {
            const int d = dist[curr][deep[curr] - deep[fa[nxt]]];
            if (len >= d)
            {
                ans++;
                ans += cnt.query(fa[nxt], len - d);
                ans -= cntFa.query(nxt, len - d);
            }
        }
        return ans >= k;
    }

    int query(const int curr) const
    {
        int l = 1, r = MX;
        while (l < r)
        {
            const int mid = l + r >> 1;
            if (check(curr, mid)) r = mid;
            else l = mid + 1;
        }
        return l;
    }
} pointTree;

inline void solve()
{
    cin >> n >> k;
    forn(i, 1, n-1)
    {
        int u, v, val;
        cin >> u >> v >> val;
        child[u].emplace_back(v, val);
        child[v].emplace_back(u, val);
    }
    pointTree.init();
    forn(i, 1, n) cout << pointTree.query(i) << 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;
}

\[预处理倍增数组:\ O(n\log{n}) \]

\[构建点分树:\ O(n\log{n}) \]

\[预处理两个容斥树状数组:\ O(n\log^2{n}) \]

\[预处理每个分治到祖先分治中心的距离数组:\ O(n\log^2{n}) \]

\[单点查询:\ 二分+点分树上暴力跳祖先分治中心+树状数组容斥查询=O(\log^3{n}) \]

\[所以总复杂度为:\ O(n\log^3{n}) \]

posted @ 2024-07-04 01:14  Athanasy  阅读(41)  评论(0编辑  收藏  举报