洛谷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;
}
洛谷P2664 树上游戏(点分治)的更多相关文章
- 洛谷P2664 树上游戏——点分治
原题链接 被点分治虐的心态爆炸了 题解 发现直接统计路径上的颜色数量很难,考虑转化一下统计方式.对于某一种颜色\(c\),它对一个点的贡献为从这个点出发且包含这种颜色的路径条数. 于是我们先点分一下, ...
- 洛谷 P2664 树上游戏 解题报告
P2664 树上游戏 题目描述 \(\text{lrb}\)有一棵树,树的每个节点有个颜色.给一个长度为\(n\)的颜色序列,定义\(s(i,j)\) 为 \(i\) 到 \(j\) 的颜色数量.以及 ...
- 洛谷P2664 树上游戏(点分治)
传送门 题解 因为一个sb错误调了一个晚上……鬼晓得我为什么$solve(rt)$会写成$solve(v)$啊!!!一个$O(logn)$被我硬生生写成$O(n)$了竟然还能过$5$个点……话说还一直 ...
- 洛谷P2664 树上游戏 【点分治 + 差分】
题目 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] 输入格式 第一行为一个整数n,表示树节点的数量 ...
- ●洛谷P2664 树上游戏
题链: https://www.luogu.org/problemnew/show/P2664题解: 扫描线,线段树维护区间覆盖 https://www.luogu.org/blog/ZJ75211/ ...
- 【刷题】洛谷 P2664 树上游戏
题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 \[sum_i=\sum_{j=1}^ns(i,j)\] 现在他想让你求出所有 ...
- 洛谷P2664 树上游戏
https://www.luogu.org/problemnew/show/P2664 #include<cstdio> #include<algorithm> #includ ...
- P2664 树上游戏
P2664 树上游戏 https://www.luogu.org/problemnew/show/P2664 分析: 点分治. 首先关于答案的统计转化成计算每个颜色的贡献. 1.计算从根出发的路径的答 ...
- Luogu P2664 树上游戏 dfs+树上统计
题目: P2664 树上游戏 分析: 本来是练习点分治的时候看到了这道题.无意中发现题解中有一种方法可以O(N)解决这道题,就去膜拜了一下. 这个方法是,假如对于某一种颜色,将所有这种颜色的点全部删去 ...
随机推荐
- 基于思科模拟器的AAA配置与验证
拓扑图: 地址表如图所示 三个路由器之间采用ospf协议达到互通 先做ping通测试 由ApingB 由ApingC 配置AAA认证 在R1上 R1(config)#username shuaiqiy ...
- GC参考手册 —— GC 调优(基础篇)
GC调优(Tuning Garbage Collection)和其他性能调优是同样的原理.初学者可能会被 200 多个 GC参数弄得一头雾水, 然后随便调整几个来试试结果,又或者修改几行代码来测试.其 ...
- asp.net core系列 59 Ocelot 构建基础项目示例
一.入门概述 从这篇开始探讨Ocelot,Ocelot是一个.NET API网关,仅适用于.NET Core,用于.NET面向微服务/服务的架构中.当客户端(web站点.ios. app 等)访问we ...
- Linux创建普通用户
声明:作者原创,转载注明出处. 作者:帅气陈吃苹果 1.创建用户,-m表示同时创建用户家目录 sudo useradd -m hadoop 2.为创建的hadoop用户设置密码 sudo passwd ...
- HTML阻止iframe跳转页面并使用iframe在页面内嵌微信网页版
昨天看到这篇文章[置顶]开源组件NanUI一周年 - 使用HTML/CSS/JS来构建.Net Winform应用程序界面 就想弄一个winform结合html5的一个小东西,突有兴致,想在里面嵌套一 ...
- 用Portable.BouncyCastle来进行加解密的代码demo
前言 这里对之前对接的公司中的代码demo做一个总结,原本为清一色的java,哈哈.这里都转成C#.用到的库是Portable.BouncyCastle.官网.之前也是准备用.net core 内置的 ...
- SpaceSyntax【空间句法】之DepthMapX学习:唠叨(目录)
最近花大力气学习了空间句法这一理论,以及其相关软件DepthMapX. 我觉得吧,你要是能搜索到这理论,这一软件名,这篇博客,那我甚至都不用介绍这软件是干什么用的——好吧,还是会说一下的. 虽然不知道 ...
- 【Android】用Cubism 2制作自己的Live2D——官方App样例源码学习(1)!
前言- 上几篇文章,我们一个一个的研究了Cubism官方提供的Android使用Live2D的简单例子,但是依旧和大家平时见到的还是有很大差距的.在研究了代码差不多一周以后,我决定还是用文字的形式记录 ...
- maven pom 引入本地jar包
maven pom 引入本地jar包 在pom.xml同级目录下新建lib文件夹,并放入本地jar包. 配置Jar包的dependency,包括groupId,artifactId,version三个 ...
- 阿里云服务器建站——centos7部署apache+mysql+php
自己也是忙活了半天,才完成了阿里云服务器的建站,这里就来分享一下. 首先如果是要自己搭建一个网站的话,除了服务器以外还要购买域名,并且要去备案,一般在哪买的域名都有备案的系统,备案的话一般要两到三个星 ...