HDU 5840 This world need more Zhu 题解

· · 个人记录

本题思路:根号分治。

设一个阙值 B,当 K > B 时暴力,若 K \le B 时则用树剖。

暴力较为简单。

而树剖的做法就是将每一个 K 的询问分组,对于每一个 K,我们将所有的点按照深度除以 K 的余数分组。而树剖的每一个查询,就相当于在查一段 DFS 序区间内深度除以 K 的余数等于一个定值的点的权值最大值。用树剖可以轻松维护。但是要注意卡常,可能是因为数据水,导致我把 B 设到 5 以下都能过。

一定注意取模问题!!!

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, B = 100;
int n, m, a[N], he[N], vc[N << 1], Ne[N << 1], tot, sz[N], Son[N], fa[N], top[N], L[N], Rk[N], dep[N], tot_dfn, ans[N];
template <class T>
inline void Rd(T &x) {
    x = 0;
    bool f = 0;
    char ch = getchar();
    while (!isdigit(ch)) f |= ch == '-', ch = getchar();
    while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    if (f)
        x = -x;
}
template <class T>  
inline void writeInt(T x) {
    char c[50];
    int cnt = 0;
    do {
        c[++cnt] = x % 10 + '0';
        x /= 10;
    } while (x);
    for (int i = cnt; i; --i) putchar(c[i]); 
}
inline void add(int x, int y) {
    vc[++tot] = y, Ne[tot] = he[x], he[x] = tot;
}
struct P {
    int l, r, p, id;
    P(int _l, int _r, int _p, int _id) {
        l = _l, r = _r, p = _p, id = _id;
    }
};
vector<P> Q[B + 5];
struct Poi {
    int id, v;
    Poi(int _id, int _v) {
        id = _id, v = _v;
    }
};
vector<Poi> b[B + 5];
void dfs1(int x) {
    sz[x] = 1, dep[x] = dep[fa[x]] + 1;
    for (int i = he[x]; i; i = Ne[i]) {
        int y = vc[i];
        if (y == fa[x]) continue;
        fa[y] = x;
        dfs1(y);
        sz[x] += sz[y];
        if (sz[y] > sz[Son[x]]) Son[x] = y;
    }
}
void dfs2(int x, int topf) {
    top[x] = topf, L[x] = ++tot_dfn, Rk[tot_dfn] = x;
    if (Son[x]) dfs2(Son[x], topf);
    for (int i = he[x]; i; i = Ne[i]) {
        int y = vc[i];
        if (y == fa[x] || y == Son[x]) continue;
        dfs2(y, y);
    }
}
int lca(int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] > dep[top[y]])
            x = fa[top[x]];
        else
            y = fa[top[y]];
    }
    return dep[x] < dep[y] ? x : y;
}
int ask(int l, int r, int K, int p) {
    int ans = 0, ll = l + ((p - dep[Rk[l]]) % K + K) % K;
    for (int i = ll; i <= r; i += K)
        ans = max(ans, a[Rk[i]]);
    return ans;
}
struct Seg {
    struct {
        int l, r, ans;
    } t[N << 2];
    inline void Up(int p) {
        t[p].ans = max(t[p << 1].ans, t[p << 1 | 1].ans);
    }
    void build(int p, int l, int r) {
        t[p].l = l, t[p].r = r;
        if (l == r) return;
        int mid(l + r >> 1);
        build(p << 1, l, mid);
        build(p << 1 | 1, mid + 1, r);
    }
    void change(int p, int x, int v) {
        if (t[p].l == t[p].r) {
            t[p].ans = v;
            return;
        }
        int mid(t[p].l + t[p].r >> 1);
        if (x <= mid) change(p << 1, x, v);
        else change(p << 1 | 1, x, v);
        Up(p);
    }
    int ask(int p, int l, int r) {
        if (l <= t[p].l && r >= t[p].r) return t[p].ans;
        int mid(t[p].l + t[p].r >> 1), val = 0;
        if (l <= mid) val = max(val, ask(p << 1, l, r));
        if (r > mid) val = max(val, ask(p << 1 | 1, l, r));
        return val;
    }
} t;
void Solution() {
    Rd(n), Rd(m);
    tot = tot_dfn = 0;
    memset(he, 0, sizeof he);
    memset(Son, 0, sizeof Son);
    for (int i = 1; i <= n; ++i) Rd(a[i]);
    for (int i = 1, x, y; i < n; ++i) {
        scanf("%d%d", &x, &y);
        add(x, y);
        add(y, x);
    }
    dfs1(1);
    dfs2(1, 1);
    for (int i = 1, x, y, K; i <= m; ++i) {
        Rd(x), Rd(y), Rd(K);
        int LCA = lca(x, y), m1 = (dep[x] + 1) % K, m2 = ((2 * dep[LCA] - dep[x] - 1) % K + K) % K;
        if (dep[x] + dep[y] - 2 * dep[LCA] + 1 < K) {
            ans[i] = 0;
            continue;
        }
        if (K > B) {
            if (K > n) ans[i] = 0;
            else {
                while (top[x] != top[y]) {
                    if (dep[top[x]] > dep[top[y]])
                        ans[i] = max(ans[i], ask(L[top[x]], L[x], K, m1)), x = fa[top[x]];
                    else
                        ans[i] = max(ans[i], ask(L[top[y]], L[y], K, m2)), y = fa[top[y]];
                }
                if (dep[x] > dep[y])
                    ans[i] = max(ans[i], ask(L[y], L[x], K, m1));
                else
                    ans[i] = max(ans[i], ask(L[x], L[y], K, m2));
            }
        } else {
            while (top[x] != top[y]) {
                if (dep[top[x]] > dep[top[y]])
                    Q[K].push_back(P(L[top[x]], L[x], m1, i)), x = fa[top[x]];
                else
                    Q[K].push_back(P(L[top[y]], L[y], m2, i)), y = fa[top[y]];
            }
            if (dep[x] > dep[y])
                Q[K].push_back(P(L[y], L[x], m1, i));
            else
                Q[K].push_back(P(L[x], L[y], m2, i));
        }
    }
    t.build(1, 1, n);
    for (int K = 1; K <= B; ++K) {
        if (Q[K].empty()) continue;
        for (int i = 0; i < K; ++i) b[i].clear();
        for (int i = 1; i <= n; ++i)
            b[dep[i] % K].push_back(Poi(L[i], a[i]));
        sort(Q[K].begin(), Q[K].end(), [](const P &a, const P &b) {
            return a.p < b.p;
        });
        for (int i = 0; i < Q[K].size(); ++i) {
            if (i == 0 || Q[K][i].p != Q[K][i - 1].p) {
                if (i) {
                    for (auto v : b[Q[K][i - 1].p])
                        t.change(1, v.id, 0);
                }
                for (auto v : b[Q[K][i].p])
                    t.change(1, v.id, v.v);
            }
            ans[Q[K][i].id] = max(ans[Q[K][i].id], t.ask(1, Q[K][i].l, Q[K][i].r));
        }
        for (auto v : b[Q[K].back().p])
            t.change(1, v.id, 0);
        Q[K].clear();
    }
    for (int i = 1; i <= m; ++i) {
        writeInt(ans[i]), putchar('\n');
        ans[i] = 0;
    }
}
int main() {
    int T;
    scanf("%d", &T);
    for (int cas = 1; cas <= T; ++cas) {
        printf("Case #%d:\n", cas);
        Solution();
    }
}