洛谷P2664 樹上遊戲(點分治)
阿新 • • 發佈:2019-04-01
chm const template pair 當前 tdi 一次 org out
題意
題目鏈接
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; }
洛谷P2664 樹上遊戲(點分治)