932F Escape Through Leaf
题目大意
https://www.luogu.org/problemnew/show/CF932F
分析
我们可以从叶子向根每次插入b和ans
所以我们不难发现就是相当于插入线段
于是李超树+线段树合并即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define int long long
const int maxm = 2e5;
const int M = 1e5;
struct node {
int k,b,le,ri;
};
node d[];
int n,m,a[],b[],rt[],ans[],cnt,sum,Ans;
bool vis[];
vector<int>v[];
inline int get(int b1,int k1,int b2,int k2){return (b2-b1)/(k1-k2);}
inline void add(int le,int ri,int wh,int B,int K){
if(!vis[wh]){
d[wh].k=K;
d[wh].b=B;
vis[wh]=;
return;
}
int f1=K*(le-M)+B,f2=d[wh].k*(le-M)+d[wh].b,f3=K*(ri-M)+B,f4=d[wh].k*(ri-M)+d[wh].b;
if(f1>=f2&&f3>=f4)return;
if(f1<=f2&&f3<=f4)d[wh].k=K,d[wh].b=B;
else {
int mid=(le+ri)>>;
int x=get(B,K,d[wh].b,d[wh].k)+M;
if(f1<=f2){
if(x>mid)d[wh].ri=(d[wh].ri?d[wh].ri:++cnt),
add(mid+,ri,d[wh].ri,d[wh].b,d[wh].k),d[wh].k=K,d[wh].b=B;
else d[wh].le=(d[wh].le?d[wh].le:++cnt),
add(le,mid,d[wh].le,B,K);
}else {
if(x<=mid)d[wh].le=(d[wh].le?d[wh].le:++cnt),
add(le,mid,d[wh].le,d[wh].b,d[wh].k),d[wh].k=K,d[wh].b=B;
else d[wh].ri=(d[wh].ri?d[wh].ri:++cnt),
add(mid+,ri,d[wh].ri,B,K);
}
}
}
inline void que(int le,int ri,int wh,int x){
if(!wh)return;
if(vis[wh])Ans=min(Ans,(x-M)*d[wh].k+d[wh].b);
if(le==ri)return;
int mid=(le+ri)>>;
if(mid>=x)que(le,mid,d[wh].le,x);
else que(mid+,ri,d[wh].ri,x);
}
inline int mer(int le,int ri,int x,int y){
if(!x||!y)return x+y;
int mid=(le+ri)>>;
d[x].le=mer(le,mid,d[x].le,d[y].le);
d[x].ri=mer(mid+,ri,d[x].ri,d[y].ri);
add(le,ri,x,d[y].b,d[y].k);
return x;
}
inline void dfs(int x,int fa){
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa){
dfs(v[x][i],x);
rt[x]=mer(,maxm,rt[x],rt[v[x][i]]);
}
Ans=1000000000000000007ll;
if(!rt[x])rt[x]=++cnt;
que(,maxm,rt[x],a[x]+M);
if(v[x].size()==&&v[x][]==fa)Ans=;
ans[x]=Ans;
add(,maxm,rt[x],ans[x],b[x]);
}
signed main(){
int i,j,k;
scanf("%lld",&n);
for(i=;i<=n;i++)scanf("%lld",&a[i]);
for(i=;i<=n;i++)scanf("%lld",&b[i]);
for(i=;i<n;i++){
int x,y;
scanf("%lld%lld",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for(i=;i<;i++)d[i].le=d[i].ri=d[i].k=d[i].b=;
dfs(,);
for(i=;i<=n;i++)printf("%lld ",ans[i]);
return ;
}
932F Escape Through Leaf的更多相关文章
- @codeforces - 932F@ Escape Through Leaf
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 个点的树(标号1~n),以结点 1 为根.每个结点 ...
- 【CF932F】Escape Through Leaf 启发式合并set维护凸包
[CF932F]Escape Through Leaf 题意:给你一棵n个点的树,每个点有树形ai和bi,如果x是y的祖先,则你可以从x花费$a_x\times b_y$的费用走到y(费用可以为负). ...
- Codeforces 932.F Escape Through Leaf
F. Escape Through Leaf time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- CF932F Escape Through Leaf
CF932F Escape Through Leaf 首先, $ O(n^2) $ dp 是很显然的,方程长这样: \[dp[u] = min\{dp[v] + a_u\times b_v\} \] ...
- Codeforces Round #463 F. Escape Through Leaf (李超线段树合并)
听说正解是啥 set启发式合并+维护凸包+二分 根本不会啊 , 只会 李超线段树合并 啦 ... 题意 给你一颗有 \(n\) 个点的树 , 每个节点有两个权值 \(a_i, b_i\) . 从 \( ...
- CF932F Escape Through Leaf 斜率优化、启发式合并
传送门 \(DP\) 设\(f_i\)表示第\(i\)个节点的答案,\(S_i\)表示\(i\)的子节点集合,那么转移方程为\(f_i = \min\limits_{j \in S_i} \{a_i ...
- CF932F Escape Through Leaf(DP,斜率优化)
SB 题. 写出 DP 方程:\(f_i\) 表示从 \(i\) 跳的最小值. \(i\) 是叶子就是 \(0\),否则就是选个子树中的 \(v\),\(f_i=\min(f_v+a_ib_v)\). ...
- 【CF 463F】Escape Through Leaf
题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...
- zhengrui集训笔记2
Day_6 计算几何 点积\Large 点积点积 叉积\Large 叉积叉积 极角\Large 极角极角 < π\piπ :叉积判断 else :atan2 旋转\Large 旋转旋转 左乘第一 ...
随机推荐
- 二分答案(Widespread )
二分答案其实是变相贪心,这周算是被这个虐了,怎么都想不到,比如这题,一直纠结在最大值的贪心上后面队友一指点,原来可以先减去x*b,然后a-b随机分配就好了, 仔细一想没错呀,每次攻击必然受到x*b次伤 ...
- springboot 不同环境不同的配置
前言 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频 ...
- jfrog artifactory jenkins pipeline 集成
1. 预备环境 artifactory ( 开源版本 ) maven jenkins jenkins artifactory plugin (在插件管理安装即可) 2. 配置artifactory ...
- 系列文章--WCF后传学习文章
WCF后传系列(10):消息处理功能核心 摘要: WCF是一个通信框架,同时也可以将它看成是一个消息处理或者传递的基础框架,它可以接收消息.对消息做处理,或者根据客户端给定的数据构造消息并将消息发送到 ...
- Indy10收发Hotmail邮件
hotmail开放了pop3,可以使用客户端工具收取邮件了. POP 服务器: pop3.live.com (端口 995)需要 POP SSL?: 是的用户名: Windows Live ID, 比 ...
- 字符串(二)(PHP)
1.大段文本在PHP中应该如果表示? 答: <?php $str = <<<aaa hello word; fjasdflj fjslad aaa;date_sub() aaa ...
- fuser
fuser 命令,查看正在被占用的文件:
- Activity小技巧
随时随地退出程序 新建一个结合类对所有活动进行管理. public class ActivityCollector{ public static List<Activity> activi ...
- SizeGripStyle 枚举
成员名称 说明 Auto 需要时会自动显示大小调整的箭头图标 Hide 大小调整的箭头图标被隐藏. (SizeGripStyle=Hide,禁用拖动窗体右下角可以改变大小的功能) Sho ...
- C++面试题(三)
1 什么是函数对象?有什么作用? 函数对象却具有许多函数指针不具有的有点,函数对象使程序设计更加灵活,而且能够实现函数的内联(inline)调用,使整个程序实现性能加速. 函数对象:这里已经说明了这是 ...