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 旋转旋转 左乘第一 ...
随机推荐
- IntelliJ IDEA下SVN配置及使用
一.在IDEA中使用SVN,首先需要下载安装 TortoiseSVN 插件. 打开 TortoiseSVN 下载地址,选择适合自己的系统类型下载. 接下来,进行安装即可.选择Modify,默认 com ...
- vc++ windows 快速启动栏创建快捷方式
创建快速启动栏 在windows软件开发中,软件安装过程中总是需要在快速启动栏创建快捷方式,下面介绍一种快速启动栏创建快捷方式的方法,具体代码如下:(该方法不支持win10,目前还没有找到win10的 ...
- Java程序员必须掌握的知识
1.语法:Java程序员必须比较熟悉语法,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息 知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项, ...
- TCP传输控制协议
TCP 在TCP/IP协议模型中, 传输层协议有TCP和UDP, 这里主要介绍下可靠传输TCP协议, 目前是传输层协议首选. 特点 面向数据流(字节流形式) 虚电路连接 有缓冲传输(提供push机制 ...
- python3 chromeDriver 安装与配置
1. 准备工作 在这之前请确保已经正确安装好了Chrome浏览器并可以正常运行,安装过程不再赘述. 2. 查看版本 点击Chrome菜单"帮助"→"关于Google Ch ...
- var ev = ev || event
event是事件对象(也是window的属性),但不是标准的,只有IE支持. 在W3C标准支持的浏览器下事件对象是引发事件函数的第一个参数,参数名随意. 所以,我们一般使用事件对象: function ...
- 关于ORM,以及Python中SQLAlchemy的sessionmaker,scoped_session
orm(object relational mapping):对象关系映射. python面向对象,而数据库是关系型. orm是将数据库关系映射为Python中的对象,不用直接写SQL. 缺点是性能略 ...
- JVM介绍(二)
1 JVM简介 JVM是我们Javaer的最基本功底了,刚开始学Java的时候,一般都是从“Hello World”开始的,然后会写个复杂点class,然后再找一些开源框架,比如Spring,Hibe ...
- ecmall在linux下的安装注意事项(转) ----ecmall系统迁移
linux+apache+mysql+php,然后自己开始在linux下安装ecmall并做迁移,整理了一下中间碰到的问题.1.系统选择的环境是centos6.3,安装不做介绍. 2.安装 MySQL ...
- linux系统无法挂载U盘
插上U盘 [ 2407.650440] usb 1-3.3: new high speed USB device number 7 using s5p-ehci [ 2407.887332] usb ...