题意

题目链接

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 树上游戏(点分治)的更多相关文章

  1. 洛谷P2664 树上游戏——点分治

    原题链接 被点分治虐的心态爆炸了 题解 发现直接统计路径上的颜色数量很难,考虑转化一下统计方式.对于某一种颜色\(c\),它对一个点的贡献为从这个点出发且包含这种颜色的路径条数. 于是我们先点分一下, ...

  2. 洛谷 P2664 树上游戏 解题报告

    P2664 树上游戏 题目描述 \(\text{lrb}\)有一棵树,树的每个节点有个颜色.给一个长度为\(n\)的颜色序列,定义\(s(i,j)\) 为 \(i\) 到 \(j\) 的颜色数量.以及 ...

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

    传送门 题解 因为一个sb错误调了一个晚上……鬼晓得我为什么$solve(rt)$会写成$solve(v)$啊!!!一个$O(logn)$被我硬生生写成$O(n)$了竟然还能过$5$个点……话说还一直 ...

  4. 洛谷P2664 树上游戏 【点分治 + 差分】

    题目 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] 输入格式 第一行为一个整数n,表示树节点的数量 ...

  5. ●洛谷P2664 树上游戏

    题链: https://www.luogu.org/problemnew/show/P2664题解: 扫描线,线段树维护区间覆盖 https://www.luogu.org/blog/ZJ75211/ ...

  6. 【刷题】洛谷 P2664 树上游戏

    题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 \[sum_i=\sum_{j=1}^ns(i,j)\] 现在他想让你求出所有 ...

  7. 洛谷P2664 树上游戏

    https://www.luogu.org/problemnew/show/P2664 #include<cstdio> #include<algorithm> #includ ...

  8. P2664 树上游戏

    P2664 树上游戏 https://www.luogu.org/problemnew/show/P2664 分析: 点分治. 首先关于答案的统计转化成计算每个颜色的贡献. 1.计算从根出发的路径的答 ...

  9. Luogu P2664 树上游戏 dfs+树上统计

    题目: P2664 树上游戏 分析: 本来是练习点分治的时候看到了这道题.无意中发现题解中有一种方法可以O(N)解决这道题,就去膜拜了一下. 这个方法是,假如对于某一种颜色,将所有这种颜色的点全部删去 ...

随机推荐

  1. 漏洞经验分享丨Java审计之XXE(下)

    上篇内容我们介绍了XXE的基础概念和审计函数的相关内容,今天我们将继续分享Blind XXE与OOB-XXE的知识点以及XXE防御方法,希望对大家的学习有所帮助! 上期回顾  ◀漏洞经验分享丨Java ...

  2. 深度辨析 Python 的 eval() 与 exec()

    Python 提供了很多内置的工具函数(Built-in Functions),在最新的 Python 3 官方文档中,它列出了 69 个. 大部分函数是我们经常使用的,例如 print().open ...

  3. [译]PEP 380--子生成器的语法

    导语: PEP(Python增强提案)几乎是 Python 社区中最重要的文档,它们提供了公告信息.指导流程.新功能的设计及使用说明等内容.对于学习者来说,PEP 是非常值得一读的第一手材料,学习中遇 ...

  4. List-LinkedList、set集合基础增强底层源码分析

    List-LinkedList 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 继上一章继续讲解,上章内容: List-ArreyLlist集合基础增强底层源码分析:https:// ...

  5. 在个人博客中优雅的使用Gitalk评论插件

    在上一篇博客<程序员如何从0到1搭建自己的技术博客>中,我们了解了如何快速的从0到1搭建一个个人博客. 其实细心的你会发现,该博客用到了一个评论插件,这个插件就是Gitalk. 如果想要在 ...

  6. 使用kubeadm平滑地升级kubenetes集群(v1.10.2到v1.10.3)

    写在前面 kubernetes是目前最炙手火热的容器管理.调度.编排平台,不仅拥有全面的生态系统,而且还开源.开放自治,谁贡献的多,谁的话语权就大,云提供商都有专门的工程师来从事kubernetes开 ...

  7. Java 10 var关键字详解和示例教程

    在本文中,我将通过示例介绍新的Java SE 10特性——“var”类型.你将学习如何在代码中正确使用它,以及在什么情况下不能使用它. 介绍 Java 10引入了一个闪亮的新功能:局部变量类型推断.对 ...

  8. Spring的PropertyPlaceholderConfigurer强制使用默认值的坑

    1.问题 dubbo client配置: <dubbo:reference id="channelCustomerClient" interface="com.gt ...

  9. class基本使用

    console.log(` 1.创建一个空对象 2.让this 指向刚刚创建好的空对象 3.执行构造函数内的代码 (为相关的属性和方法赋值) 4.返回创建好的对象`) // 1.创建一个空对象 // ...

  10. appium-desktop定位元素原理

    初衷 最近在编写Android App自动化用例,其中元素定位相对来说耗费的时间比较长.我们都知道Appium-desktop拥有自己的录制功能,我们就在想是不是可以把录制功能跟我司的自动化框架(AT ...