传送门

题目大意

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的更多相关文章

  1. @codeforces - 932F@ Escape Through Leaf

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 个点的树(标号1~n),以结点 1 为根.每个结点 ...

  2. 【CF932F】Escape Through Leaf 启发式合并set维护凸包

    [CF932F]Escape Through Leaf 题意:给你一棵n个点的树,每个点有树形ai和bi,如果x是y的祖先,则你可以从x花费$a_x\times b_y$的费用走到y(费用可以为负). ...

  3. Codeforces 932.F Escape Through Leaf

    F. Escape Through Leaf time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  4. CF932F Escape Through Leaf

    CF932F Escape Through Leaf 首先, $ O(n^2) $ dp 是很显然的,方程长这样: \[dp[u] = min\{dp[v] + a_u\times b_v\} \] ...

  5. Codeforces Round #463 F. Escape Through Leaf (李超线段树合并)

    听说正解是啥 set启发式合并+维护凸包+二分 根本不会啊 , 只会 李超线段树合并 啦 ... 题意 给你一颗有 \(n\) 个点的树 , 每个节点有两个权值 \(a_i, b_i\) . 从 \( ...

  6. CF932F Escape Through Leaf 斜率优化、启发式合并

    传送门 \(DP\) 设\(f_i\)表示第\(i\)个节点的答案,\(S_i\)表示\(i\)的子节点集合,那么转移方程为\(f_i = \min\limits_{j \in S_i} \{a_i ...

  7. CF932F Escape Through Leaf(DP,斜率优化)

    SB 题. 写出 DP 方程:\(f_i\) 表示从 \(i\) 跳的最小值. \(i\) 是叶子就是 \(0\),否则就是选个子树中的 \(v\),\(f_i=\min(f_v+a_ib_v)\). ...

  8. 【CF 463F】Escape Through Leaf

    题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...

  9. zhengrui集训笔记2

    Day_6 计算几何 点积\Large 点积点积 叉积\Large 叉积叉积 极角\Large 极角极角 < π\piπ :叉积判断 else :atan2 旋转\Large 旋转旋转 左乘第一 ...

随机推荐

  1. 剑指offer-第五章优化时间和空间效率(两个链表的第一个公共节点)

    思路1:要求的是两个链表的第一个公共节点,首先想到的是用栈来存放两个链表,然后依次从栈中抛出,直到最后一个相同的节点为止.但是要用到两个栈,空间复杂度为O(n): 思路2:从头到尾分别遍历两个链表得到 ...

  2. cookie跨站脚本漏洞解决方案

    近日项目碰到一个跨脚本注入的问题: 这安全测评工具也是厉害了,直接将脚本注入到cookie里头,以前没有碰到这样的情况. 之前写过一篇文章过滤跨脚本注入的问题.<浅谈XSS攻击原理与解决方法&g ...

  3. Java 多个引用类型变量引用同一个对象

    引用类型变量在声明后必须引用对象才能使用. 一个引用变量只能唯一指向一个对象,但同一个对象可被多个引用类型变量引用. 如:MyDate today; //将变量跟配给一个保存引用的空间(栈) toda ...

  4. 【转】java接口的性能测试

    这周尝试了一把性能测试,之前都是测试网站的性能测试,java接口的性能测试还是头一次,学到了很多,特此分享一下. 主要用到了两个性能测试工具,一个是jmeter,一个是LoadRunner. 使用jm ...

  5. 如何检测 51单片机IO口的下降沿

    下降沿检测,说白了就是满足这样一个逻辑,上次检测是1,这次检测是0,就是下降沿. 从这个条件可知,要确保能够正确检测到一个下降沿,负脉冲的宽度,必须大于一个检测周期,当负脉冲宽度小于一个检测周期,就有 ...

  6. 编写一个jQuery的扩展方法(插件)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. scikit-learn和tensorflow的区别

    1.功能不同 Scikit-learn(sklearn)的定位是通用机器学习库,而TensorFlow(tf)的定位主要是深度学习库.一个显而易见的不同:tf并未提供sklearn那种强大的特征工程, ...

  8. EasyUI应用总结

    1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncod ...

  9. Full Binary Tree(sdut 2882)

    Problem Description: In computer science, a binary tree is a tree data structure in which each node ...

  10. kibana-4.6.3-linux-x86_64.tar.gz的下载(图文详解)

    对于Kibana ,我们知道,是Elasticsearch/Logstash/Kibana的必不可少成员. 第一步:进入Elasticsearch的官网 https://www.elastic.co/ ...