洛谷P2664 树上游戏(点分治)

题意

题目链接

Sol

神仙题。。Orz yyb

考虑点分治,那么每次我们只需要统计以当前点为\(LCA\)的点对之间的贡献以及\(LCA\)到所有点的贡献。

一个很神仙的思路是,对于任意两个点对的路径上的颜色,我们只统计里根最近的那个点的贡献。

有了这个思路我们就可以瞎搞了,具体的细节很繁琐,但是大概思路是事实维护每个点的子树中的点会产生的贡献。比如某个点的颜色在它到根的路径上第一次出现,那么它子树中的所有点\(siz[x]\),都会对外面的点产生贡献。

统计子树的时候只需要先消除掉子树的影响,然后dfs的时候考虑一下新加的颜色的贡献。。

复杂度\(O(n \log n)\)

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
#define pb push_back 
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, c[MAXN], cnt[MAXN], vis[MAXN], siz[MAXN], Lim, mx[MAXN], root;
LL ans[MAXN], num[MAXN], Sum;
vector<int> v[MAXN];
void FindRoot(int x, int fa) {
    siz[x] = 1; mx[x] = 1;
    for(auto &to : v[x]) {
        if(to == fa || vis[to]) continue;
        FindRoot(to, x);
        siz[x] += siz[to];
        chmax(mx[x], siz[to]);
    }
    chmax(mx[x], Lim - siz[x]);
    if(mx[x] < mx[root]) 
        root = x;
}

void dfs(int x, int fa, int opt) {
    cnt[c[x]]++;
    if(cnt[c[x]] == 1) Sum += siz[x] * opt, num[c[x]] += siz[x] * opt;
    for(auto &to : v[x])
        if(to != fa && !vis[to]) dfs(to, x, opt);
    cnt[c[x]]--;
}
void calc(int x, int fa) {
    cnt[c[x]]++;
    if(cnt[c[x]] == 1) Sum += Lim - num[c[x]];
    ans[x] += Sum;
    for(auto &to : v[x]) {
        if(to == fa || vis[to]) continue;
        calc(to, x);
    }
    cnt[c[x]]--;
    if(cnt[c[x]] == 0) Sum -= Lim - num[c[x]];
}
void Divide(int x) {
    if(vis[x]) return ; vis[x] = 1;
    Sum = 0; FindRoot(x, 0);
    dfs(x, 0, 1); ans[x] += Sum;
    for(auto &to : v[x]) {
        if(vis[to]) continue;
        num[c[x]] -= siz[to]; Sum -= siz[to]; Lim -= siz[to];
        cnt[c[x]] = 1; dfs(to, x, -1); cnt[c[x]] = 0;
        calc(to, x);
        cnt[c[x]] = 1; dfs(to, x, 1); cnt[c[x]] = 0;
        num[c[x]] += siz[to]; Sum += siz[to]; Lim += siz[to];
    }
    dfs(x, 0, -1);
    for(auto &to : v[x]) 
        if(!vis[to]) {
            root = 0, Lim = siz[to], FindRoot(to, x);
            Divide(root);
    }
}
signed main() {
    //freopen("a.in", "r", stdin);freopen("b.out", "w", stdout);
    N = read(); mx[0] = 1e9;
    for(int i = 1; i <= N; i++) c[i] = read();
    for(int i = 1; i < N ; i++) {
        int x = read(), y = read();
        v[x].pb(y); v[y].pb(x);
    }
    Lim = N; root = 0; FindRoot(1, 0);
    Divide(root);
    for(int i = 1; i <= N; i++) cout << ans[i] << '\n';
    return 0;
}
posted @ 2019-04-01 08:58 自为风月马前卒 阅读(...) 评论(...) 编辑 收藏

Contact with me

……