2020牛客多校第一场B(虚树)
#include<cstdio>
typedef long long ll;
const int N = 2e5 + 50;
int n, cnt, top, tot;
int c[N], mindiv[N], dep[N], w[N];
int head[N], sta[N], lca_dep[N];
ll minn, ans[N];
struct Edge{
int nex, to;
}e[N << 1];
inline int lowbit(int &x) { return x & -x; }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline void add(int a, int b) { e[++cnt] = {head[a], b}; head[a] = cnt; }
void add(int x){
while(x <= n){
++c[x];
x += lowbit(x);
}
}
int ask(int x){
int ans = 0;
while(x){
ans += c[x];
x -= lowbit(x);
}
return ans;
}
void init(int n){
cnt = 0;
for(int i = 1; i <= n; ++i) c[i] = w[i] = dep[i] = lca_dep[i] = ans[i] = 0;
}
void pre(int n){
for(int i = 2; i <= n; ++i)
for(int t = i; t <= n; t += i)
if(!mindiv[t]) mindiv[t] = i;
}
void build(){
tot = n;
sta[top = 1] = 1, head[1] = 0;
for(int i = 2, t; i <= n; ++i){
t = i; dep[i] = dep[i - 1] + 1;
while(t != mindiv[t]) ++dep[i], t /= mindiv[t];
lca_dep[i] = ask(n) - ask(t - 1);
for(int t = i; t != 1; t /= mindiv[t]) add(mindiv[t]);
}
for(int i = 2; i <= n; ++i){
//printf("%d %d\n", lca_dep[i], sta[1]);
while(top > 1 && dep[sta[top - 1]] >= lca_dep[i]){
add(sta[top - 1], sta[top]);
--top;
}
if(dep[sta[top]] != lca_dep[i]){
dep[++tot] = lca_dep[i];
head[tot] = 0;
add(tot, sta[top]);
sta[top] = tot;
}
head[i] = 0, sta[++top] = i;
}
//printf("%d %d ", top, sta[1]);
for(int i = 1; i < top; ++i) add(sta[i], sta[i + 1]);
}
void dfs(int u){
//printf("%d ", u);
ans[1] += 1LL * w[u] * dep[u];
for(int i = head[u]; i; i = e[i].nex){
int to = e[i].to;
dfs(to);
w[u] += w[to];
}
}
void dfs2(int u){
for(int i = head[u]; i; i = e[i].nex){
int to = e[i].to;
ans[to] = ans[u] + (w[1] - 2LL * w[to]) * (dep[to] - dep[u]);
dfs2(to);
}
}
int main(){
pre(1e5);
while(scanf("%d", &n) != EOF){
for(int i = 1; i <= n; ++i) scanf("%d", &w[i]);
minn = 1e15;
build();
dfs(1); dfs2(1);
for(int i = 1; i <= tot; ++i) minn = min(ans[i], minn);
printf("%lld\n", minn);
init(tot);
}
return 0;
}
2020牛客多校第一场B(虚树)的更多相关文章
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 2019牛客多校第一场E ABBA(DP)题解
链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...
- 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)
Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...
- 2019牛客多校第一场A-Equivalent Prefixes
Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...
- 2019年牛客多校第一场 I题Points Division 线段树+DP
题目链接 传送门 题意 给你\(n\)个点,每个点的坐标为\((x_i,y_i)\),有两个权值\(a_i,b_i\). 现在要你将它分成\(\mathbb{A},\mathbb{B}\)两部分,使得 ...
- 2019年牛客多校第一场 H题XOR 线性基
题目链接 传送门 题意 求\(n\)个数中子集内所有数异或为\(0\)的子集大小之和. 思路 对于子集大小我们不好维护,因此我们可以转换思路变成求每个数的贡献. 首先我们将所有数的线性基的基底\(b\ ...
- 2019牛客多校第一场 A.Equivalent Prefixes
题目描述 Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r ...
- 2019 牛客多校第一场 D Parity of Tuples
题目链接:https://ac.nowcoder.com/acm/contest/881/D 看此博客之前请先参阅吕凯飞的论文<集合幂级数的性质与应用及其快速算法>,论文中很多符号会被本文 ...
随机推荐
- Python第三方库pydash功能介绍
Python第三方库pydash功能介绍 本文来自ChatGPT的回答整理 demo部分都验证过ok 介绍 pydash 是一个 Python 库,用于提供类似于 JavaScript 库 lodas ...
- 部署属于自己的New bing Ai
该项目来源 https://github.com/adams549659584/go-proxy-bingai 项目体验地址 https://bing.vcanbb.top/web/#/ 项目介绍 基 ...
- Android源码-生成系统签名文件
一.生成keystore签名文件 android 源码目录build\target\product\security 取platform.pk8 platform.x509.pem放到一个目录下 生成 ...
- 如何调用api接口获取到商品数据
要调用API接口获取商品数据,需要进行以下步骤: 确定API接口 首先需要确定要使用的API接口,可以通过搜索引擎或者相关文档来查找适合的API接口.以淘宝开放平台为例,可以使用淘宝的商品信息查询AP ...
- 如何使用关键词搜索API接口获取到快手的商品
如果您想使用关键词搜索API接口获取到快手的商品,可以通过以下步骤实现: 1. 首先注册账号.根据文档申请相应的接口权限. 2. 确定需要使用的API接口.对于商品搜索,您可以查看相关的API文档以获 ...
- KRPANO PR10最新激活码(破解)分享
KRPano pr10最新版本激活码下载地址: http://pan.baidu.com/s/1qYv2vO4 适用于最新pr10以及之前版本,解压密码为KRPano技术解密群群号:551278936 ...
- Abp vNext 模块加载机制
文章目录 生命周期 PreConfigureServices 添加依赖注入或者其它配置之前 ConfigureServices 添加依赖注入或者其它配置 PostConfigureServices 添 ...
- std::copy与std::back_inserter引发的惨案
#include <iostream> #include <vector> #include <numeric> #include <sstream> ...
- C#学习笔记--复杂数据类型、函数和结构体
C#基础 复杂数据类型 特点:多个数据变量地一个集合体,可以自己命名 种类:枚举.数组和结构体 枚举:整型常量的集合 数组:任意变量类型的顺序存储的数据集合 结构体:任意变量类型的数据组合成的数据块 ...
- Java 深度优先搜索 and 广度优先搜索的算法原理和代码展示
111. 二叉树的最小深度 题目:给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 方法1:深度优先搜索 原理:深度优先搜 ...