P2441 角色属性树

· · 个人记录

题解怎么全是暴力啊,写一下复杂度看起来比较对的做法,希望给过一下。

题意:给定一棵以 i 为根的树,每个点有点权 a_i,多次询问问 fa_i 到根的路径上深度最大且 \gcd(a_i,a_j)\gt 1 的点 j,需要支持极少次数的点权修改。

思路:注意到我们只需要判断是否互质就好了,即有没有一个大于 1 的因数,于是可以转换成询问一个存在公共质因子的点,这个转化是必要的。

于是我们有了一个暴力:令 f_{i,j} 表示从根到 i 质因子 j 出现的深度最大的位置,分解质因数即可处理,询问对其父亲的所有质因子 j 拿出 f_{i,j} 判断可行性,最后取最大值即可。

写出来应该是这个样子。

inline void dfs(int u, int fr) {
    dep[u] = dep[fr] + 1, fa[u] = fa[fr];
    for (int i = 2; i <= V; i++) // V: max a[i]
        f[u][i] = f[fr][i];
    for (int p : fac[u]) // fac[u]: prime factors of a[u]
        f[u][p] = u;
    for (int v : g[u]) 
        if (v != fr)
            dfs(v, u);
}
inline int query(int u) {
    int res = 0;
    for (int p : fac[u]) {
        int x = f[fa[u]][p];
        if (dep[x] > dep[res])
            res = x;
    }
    return res;
}

修改重构就好了。

令 T 表示修改次数,复杂度 O(nV^{\frac{1}{2}}+TnV+m\omega(V))。

这个东西时间空间全部爆炸,想想能不能优化。

真巧,可以优化。

分解质因数直接 Pollard-Rho 做一下。

我们每次实际上是从 fa_u 的前缀 f 上加入至多 \omega(V) 个质因子,这个用一个主席树维护可以做,于是单次重构复杂度变成了 O(n\omega(V)\log V),单次查询复杂度变成了 O(\omega(V)\log V)。

好像可以过了,不过是因为随机数据每个数的 \omega 的期望较小,构造一组点权为 2\times 3\times 5\times 7\times 11\times 13\times 17\times 19\times p 的数据即可卡满,其中 p 是一个随机的小质数。

而对于这种数据我们的第一种暴力好像又有用武之地了:因为 \omega 比较大的时候每个质因子一定会很小。一种暴力值域大了慢,另一种优化后值域小了反而慢,于是考虑平衡。

下面这个阈值分治(我不知道是不是叫这个)思路来自Dimly。

注意到对于 V 以内的任意一个数,它至多拥有 \lfloor\log B-1\rfloor 个大于 B 的质因子。

先随便取一个 B,对于小于等于 B 的质因子我们用 f 数组记录,对于其他的因子我们跑主席树,于是复杂度变成了 O(n^{\frac{1}{4}}+Tn(\omega(V)+\frac{B}{\log B}+\log B\log V)+m(\omega(V)+\log B\log V))。好难看。

然后视两边级别取一个尽量平衡的数就好了,比如 B=256,此时主席树插入不超过三次,而 f 需要记录的数也就五十几个,容易发现再大一点也没多大用。

于是可以写出这样的代码(注意主席树的区间需要开 long long,否则 mid 会溢出,我被这个东西卡爆栈了还傻傻的找管理员开大内存):

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5 + 5, inf = INT_MAX, B = 256;
unordered_map<ll, int> fac[N];
int n, m, idx, dep[N], fa[N], a[N], f[N][55], rt[N], ls[N * 64], rs[N * 64], pos[N * 64];
int fir[N], nxt[N << 1], to[N << 1], cnt;
int is_pr[B + 5], ord;
namespace rho {
    inline ll mul(ll x, ll y, ll p) {
        return x * y % p;
    }
    inline ll f(ll x, ll y, ll p) {
        return (mul(x, x, p) + y) % p;
    }
    inline ll qpow(ll x, ll y, ll p) {
        ll res = 1;
        while (y) {
            if (y & 1)
                res = mul(res, x, p);
            x = mul(x, x, p), y >>= 1;
        }
        return res;
    }
    const ll test[] = {2, 7, 61};
    inline bool check(ll a, ll p) {
        if (a == p)
            return 0;
        ll d = p - 1, get = qpow(a, d, p);
        if (get != 1)
            return 1;
        while ((d & 1) ^ 1)
            if (d >>= 1, (get = qpow(a, d, p)) == p - 1)
                return 0;
            else if (get != 1)
                return 1;
        return 0;
    }
    inline bool miller_rabin(ll p) {
        for (ll a : test)
            if (check(a, p)) return 0;
        return 1;
    }
    inline ll gcd(ll a, ll b) {
        if (!a) return b;
        if (!b) return a;
        ll t = __builtin_ctzll(a | b);
        a >>= __builtin_ctzll(a);
        do {
            b >>= __builtin_ctzll(b);
            if (a > b)
                swap(a, b);
            b -= a;
        } while (b);
        return a << t;
    }
    inline ll pollard_rho(ll x) {
        for (ll i = 0; i < 3; i++)
            if (!x % test[i])
                return test[i];
        ll pre = rand() % (x - 1) + 1, c = rand() % (x - 1) + 1, now = f(pre, c, x), q = 1, ret = 1;
        for (ll k = 2; ; k <<= 1, pre = now, q = 1) {
            for (ll i = 1; i <= k; i++) {
                now = f(now, c, x);
                q = mul(q, abs(now - pre), x);
            }
            ret = gcd(x, q);
            if (ret > 1) return ret;
        }
    }
    inline void solve(ll d, unordered_map<ll, int> &res) {
        if (d <= 1) return;
        if (miller_rabin(d)) {
            ++res[d];
            return;
        } else {
            ll fac_d = d;
            while (fac_d >= d)
                fac_d = pollard_rho(d);
            while (d % fac_d == 0)
                d /= fac_d;
            solve(fac_d, res);
            solve(d, res);
        }
    }
}
inline int modify(int pre, ll l, ll r, int x, int k) {
    int p = ++idx;
    ls[p] = ls[pre], rs[p] = rs[pre], pos[p] = pos[pre];
    if (l == r) {
        pos[p] = k;
        return p;
    }
    int mid = (l + r) >> 1;
    if (x <= mid)
        ls[p] = modify(ls[pre], l, mid, x, k);
    else
        rs[p] = modify(rs[pre], mid + 1, r, x, k);
    return p;
}
inline int query(int p, ll l, ll r, int x) {
    if (!p)
        return 0;
    if (l == r)
        return pos[p];
    int mid = (l + r) >> 1;
    if (x <= mid)
        return query(ls[p], l, mid, x);
    return query(rs[p], mid + 1, r, x);
}
inline void add(int u, int v) {
    to[++cnt] = v;
    nxt[cnt] = fir[u];
    fir[u] = cnt;
}
inline void dfs(int u, int fr) {
    fa[u] = fr, dep[u] = dep[fr] + 1;
    rt[u] = rt[fr];
    for (int i = 1; i <= ord; i++)
        f[u][i] = f[fr][i];
    for (auto p : fac[u]) {
        if (p.first <= B)
            f[u][is_pr[p.first]] = u;
        else
            rt[u] = modify(rt[u], 1, inf, p.first, u);
    }
    for (int i = fir[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v != fr) {
            dfs(v, u);
        }
    }
}
inline void rebuild(int u) {
    dfs(u, fa[u]);
}
signed main() {
    srand(1u * time(NULL));
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        rho::solve(a[i], fac[i]);
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v);
    }
    for (int i = 2; i <= B; i++) {
        bool flg = 0;
        for (int j = 2; j * j <= i; j++)
            if (i % j == 0) {
                flg = 1;
                break;
            }
        if (flg == 0)
            is_pr[i] = ++ord;
    }
    cerr << ord << '\n';
    rebuild(1);
    while (m--) {
        int op, u, v;
        cin >> op >> u;
        if (op == 1) {
            int fu = fa[u], res = 0;
            for (auto p : fac[u]) {
                int x;
                if (p.first <= B)
                    x = f[fu][is_pr[p.first]];
                else
                    x = query(rt[fu], 1, inf, p.first);
                if (dep[x] > dep[res])
                    res = x;
            }
            if (res == 0)
                cout << "-1\n";
            else
                cout << res << '\n';
        } else {
            cin >> v;
            fac[u].clear();
            a[u] = v;
            rho::solve(a[u], fac[u]);
            rebuild(u);
        }
    }
    return 0;
}

很长吧,还是可以卡这个代码的常数。

首先主席树的操作可以从 O(\log V) 变成 O(\log n\omega(V)),这个性能差距大概是 \frac{2}{3}。

其次每次重构从一个 size 很大的节点开始重构可以卡爆内存,需要动态地回收,但是这样会超时。所以设置一个随机的小常数 \alpha,如果重构节点在 \frac{n}{\alpha} 级别时直接整个重构,防卡。但是我觉得后面这种做法是不够严谨的,还是希望管理员调大时间限制让前面那种过。

最后单次询问是可以 O(1) 的。

最终代码如下,没有做到最小的常数,如果过不了可以尝试手写 vector 和各种调参。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5 + 5, B = 50;
const double fix = 1.4514;
struct custom_hash {
    ull operator () (int x) const {
        static const ull seed = *new unsigned short, k = *new unsigned char, mod = 10114514123ull;
        return (k * seed + x) % mod;
    }
};
unordered_set<int, custom_hash> fac[N], aft[B + 1];
vector<int> fac_v[N], aft_v[B + 1];
int n, m, idx, tot, dep[N], fa[N], a[N], f[N][B], siz[N], rt[N], ls[N * 256], rs[N * 256], ans[N];
int fir[N], nxt[N << 1], to[N << 1], cnt;
int pr[N * 10], ord, lim;
struct Q {
    int op, u, v;
} q[N];
namespace rho {
    inline int mul(int x, int y, int p) {
        return 1ll * x * y % p;
    }
    inline int f(int x, int y, int p) {
        return (1ll * mul(x, x, p) + y) % p;
    }
    inline int qpow(int x, int y, int p) {
        int res = 1;
        while (y) {
            if (y & 1)
                res = mul(res, x, p);
            x = mul(x, x, p), y >>= 1;
        }
        return res;
    }
    const int test[] = {2, 7, 61};
    inline bool check(int a, int p) {
        if (a == p)
            return 0;
        int d = p - 1, get = qpow(a, d, p);
        if (get != 1)
            return 1;
        while ((d & 1) ^ 1)
            if (d >>= 1, (get = qpow(a, d, p)) == p - 1)
                return 0;
            else if (get != 1)
                return 1;
        return 0;
    }
    inline bool miller_rabin(int p) {
        for (int a : test)
            if (check(a, p)) return 0;
        return 1;
    }
    inline int gcd(int a, int b) {
        if (!a) return b;
        if (!b) return a;
        int t = __builtin_ctz(a | b);
        a >>= __builtin_ctz(a);
        do {
            b >>= __builtin_ctz(b);
            if (a > b)
                swap(a, b);
            b -= a;
        } while (b);
        return a << t;
    }
    inline int pollard_rho(int x) {
        for (int i = 0; i < 3; i++)
            if (!x % test[i])
                return test[i];
        int pre = rand() % (x - 1) + 1, c = rand() % (x - 1) + 1, now = f(pre, c, x), q = 1, ret = 1;
        for (int k = 2; ; k <<= 1, pre = now, q = 1) {
            for (int i = 1; i <= k; i++) {
                now = f(now, c, x);
                q = mul(q, abs(now - pre), x);
            }
            ret = gcd(x, q);
            if (ret > 1) return ret;
        }
    }
    inline void solve(int d, unordered_set<int, custom_hash> &res) {
        if (d <= 1) return;
        if (miller_rabin(d)) {
            res.insert(d);
            return;
        } else {
            int fac_d = d;
            while (fac_d >= d)
                fac_d = pollard_rho(d);
            while (d % fac_d == 0)
                d /= fac_d;
            solve(fac_d, res);
            solve(d, res);
        }
    }
}
inline int modify(int pre, int l, int r, int x, int k) {
    int p = idx + 1;
    while (l < r) {
        ls[++idx] = ls[pre], rs[idx] = rs[pre];
        int mid = (l + r) >> 1;
        if (x <= mid)
            r = mid, ls[idx] = idx + 1, pre = ls[pre];
        else
            l = mid + 1, rs[idx] = idx + 1, pre = rs[pre];
    }
    ls[++idx] = k;
    return p;
}
inline int query(int p, int l, int r, int x) {
    while (l < r) {
        if (!p)
            return 0;
        int mid = (l + r) >> 1;
        if (x <= mid)
            r = mid, p = ls[p];
        else
            l = mid + 1, p = rs[p];
    }
    return ls[p];
}
inline void add(int u, int v) {
    to[++cnt] = v;
    nxt[cnt] = fir[u];
    fir[u] = cnt;
}
inline int cmp(int x, int y) {
    return dep[x] > dep[y] ? x : y;
}
inline void dfs(int u, int fr) {
    fa[u] = fr, dep[u] = dep[fr] + 1, siz[u] = 1;
    rt[u] = rt[fr];
    for (int i = 0; i < lim; i++)
        f[u][i] = f[fr][i];
    ans[u] = 0;
    for (auto j : fac_v[u]) {
        if (j <= lim)
            ans[u] = cmp(ans[u], f[u][j - 1]), f[u][j - 1] = u;
        else
            ans[u] = cmp(ans[u], query(rt[fr], 1, ord, j)), rt[u] = modify(rt[u], 1, ord, j, u);
    }
    for (int i = fir[u]; i; i = nxt[i]) {
        int v = to[i];
        if (v != fr) {
            dfs(v, u);
            siz[u] += siz[v];
        }
    }
}
inline void rebuild(int u) {
    if (siz[u] > n / fix)
        idx = 0, dfs(1, 0);
    else
        dfs(u, fa[u]);
}
signed main() {
    srand(1u * time(NULL));
    cin.tie(nullptr)->sync_with_stdio(false);
    cout.tie(nullptr);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++) {
        rho::solve(a[i], fac[i]);
        for (auto j : fac[i])
            pr[++ord] = j;
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v);
    }
    for (int i = 1; i <= m; i++) {
        cin >> q[i].op >> q[i].u;
        if (q[i].op == 2) {
            cin >> q[i].v;
            tot++;
            rho::solve(q[i].v, aft[tot]);
            for (auto j : aft[tot])
                pr[++ord] = j;
        }
    }
    sort(pr + 1, pr + ord + 1);
    ord = unique(pr + 1, pr + ord + 1) - pr - 1;
    for (int i = 1; i <= n; i++)
        for (auto j : fac[i])
            fac_v[i].emplace_back(lower_bound(pr + 1, pr + ord + 1, j) - pr);
    for (int i = 1; i <= tot; i++)
        for (auto j : aft[i])
            aft_v[i].emplace_back(lower_bound(pr + 1, pr + ord + 1, j) - pr);
    cerr << ord << '\n';
    lim = min(B, ord);
    rebuild(1);
    tot = 0;
    for (int i = 1; i <= m; i++) {
        int op = q[i].op, u = q[i].u, v = q[i].v;
        if (op == 1) {
            cout << ((ans[u]) ? (ans[u]) : (-1)) << '\n';
        } else {
            a[u] = v;
            fac_v[u] = aft_v[++tot];
            rebuild(u);
        }
    }
    return 0;
}