题意

题目链接

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核心技术梳理-集合

    一.前言 在日常开发中,我们经常会碰到需要在运行时才知道对象个数的情况,这种情况不能使用数组,因为数组是固定数量的,这个时候我们就会使用集合,因为集合可以存储数量不确定的对象. 集合类是特别有用的工具 ...

  2. [转载]学习Javascript闭包(Closure)

    学习Javascript闭包(Closure)     源地址: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures ...

  3. java实现开根号算法

    public static void main(String[] args) { long start = System.currentTimeMillis(); double target=9876 ...

  4. 深入理解令牌认证机制(token)

    以前的开发模式是以MVC为主,但是随着互联网行业快速的发展逐渐的演变成了前后端分离,若项目中需要做登录的话,那么token成为前后端唯一的一个凭证. token即标志.记号的意思,在IT领域也叫作令牌 ...

  5. angular-froala-wysiwyg 富文本编辑器使用及遇到的坑

    介绍: angular-froala-wysiwyg: froala editor 的angular版本,支持Angular 2, Angular 4, Angular 5, Angular 6 an ...

  6. 学习python的第四天

    4.29自我总结 一.Jupyter的安装以及运行 1.Jupyter的安装 运行CMD,在CMD中输入pip3 --default-timeout=100 install -U jupyter 再输 ...

  7. 自学WEB前端到什么程度才能就业

    做过多年web前端从业者,回答下这个问题 首先,这个问题主要问:自学web前端技术,如果才能找到一份web前端的工作.按照现在的招聘标准来看,无论你去哪个公司面试,你只需要满足他们公司的需求就可以. ...

  8. 当心Azure跨区域数据传输产生额外费用

    最近同事发现Azure上一台虚拟机的费用环比增加了一部分.后面仔细检查发现费用来自数据传输, 因为这是早期部署的一台Azure虚拟机(Iaas),我们在本地生成备份,然后通过AzCopy到存储账号的B ...

  9. C语言中的位段(位域)知识

    在结构体或类中,为了节省成员的存储空间,可以定义某些由位组成的字段,这些字段可以不需要以byte为单位. 这些不同位长度的字段实际存储于一个或多个整形变量.位段成员必须声明为int, signed i ...

  10. Unicode 与 utf8 utf16 utf32的关系

    Unicode是计算机领域的一项行业标准,它对世界上绝大部分的文字的进行整理和统一编码,Unicode的编码空间可以划分为17个平面(plane),每个平面包含2的16次方(65536)个码位.17个 ...