听说正解是啥 set启发式合并+维护凸包+二分 根本不会啊 , 只会 李超线段树合并 啦 ...

题意

给你一颗有 \(n\) 个点的树 , 每个节点有两个权值 \(a_i, b_i\) .

从 \(u\) 跳到 \(v\) 的代价是 \(a_u \times b_v\) . 你需要计算每个节点跳到叶子的最小代价 .

\((n \le 10^5, -10^5 \le a_i, b_i \le 10^5)\)

题解

我们首先考虑一个很容易的 \(dp\) , 令 \(dp_i\) 为 \(i\) 跳到叶子的最小代价 .

那么显然有一个转移 此处 \(v\) 是 \(u\) 的后代 .

\[\displaystyle dp_u = \min_v \{a[u] \times b[v] + dp_v\}
\]

暴力转移是 \(O(n^2)\) 的显然无法接受 .

那么考虑优化 , 不难发现这个转移就是 李超线段树上求多条直线 \(y=kx+b\) 在 \(x=k\) 最值的形式 .

\((k = b[v], x = a[u], b=dp_v)\)

那么显然可以考虑用李超线段树维护这个 \(dp\) .

对于树上的每个点 , 可以用一颗李超线段树维护这个点子树的所有直线信息 .

然后我们只需要考虑合并几颗子树信息了 , 不难发现是 套路的 线段树合并 . (这样时间空间复杂度都正确了?)

我们直接同时遍历两颗线段树 , 然后把其中一颗当前区间的优势直线暴力插入另外一颗线段树 .

最后把子树全都合并上来后 , 直接询问出 \(dp_u\) 就行了 , 然后再插入到这颗线段树中去 .

由于询问 \(a_i\) 可能为负数 , 而线段树不太好维护负数 , 我们考虑插入直线和询问的时候都向右平移 \(lim = 10^5\) 长度 .

原来的直线 \(y=kx+b\) 就变成了 \(y'=k(x-lim)+b=kx+(b-k\cdot lim)\) 了 .

(也许可以维护负数 diversion 说可以)

最后瞎分析时间复杂度 \(O(\sum minsize \log n) = O(n \log ^2 n)\) ... (错了的话大佬帮我指正啊qwq)

这道题还是比较好写的 ...

代码

#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ':' << x << endl
using namespace std; inline bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
inline bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;} inline int read() {
int x = 0, fh = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * fh;
} void File() {
#ifdef zjp_shadow
freopen ("F.in", "r", stdin);
freopen ("F.out", "w", stdout);
#endif
} const int N = 2e5 + 1e3, Lim = 1e5 + 5;
typedef long long ll; struct Line { ll k, b; int id; ll func(int x) { return k * x + b; } }; inline bool Cmp(Line a, Line b, int x) { if (!a.id) return true; return a.func(x) > b.func(x); } int rt[N]; const int Maxn = N * 30;
#define lson ls[o], l, mid
#define rson rs[o], mid + 1, r
struct Chao_Segment_Tree {
Line Adv[Maxn]; int ls[Maxn], rs[Maxn], Size; Chao_Segment_Tree () {Size = 0;}; void Insert(int &o, int l, int r, Line uv) {
if (!o) o = ++ Size;
int mid = (l + r) >> 1;
if (Cmp(Adv[o], uv, mid)) swap(Adv[o], uv); if (l == r || Adv[o].k == uv.k || !uv.id) return ;
double x = (double)(Adv[o].b - uv.b) / (uv.k - Adv[o].k);
if (x < l || x > r) return ; if (uv.k > Adv[o].k) Insert(lson, uv); else Insert(rson, uv);
} int Merge(int x, int y, int l, int r) {
if (!x || !y) return x + y;
Insert(x, l, r, Adv[y]);
int mid = (l + r) >> 1;
ls[x] = Merge(ls[x], ls[y], l, mid);
rs[x] = Merge(rs[x], rs[y], mid + 1, r);
return x;
} Line Query(int o, int l, int r, int qp) {
if (l == r) return Adv[o];
int mid = (l + r) >> 1;
Line tmp = (qp <= mid) ? Query(lson, qp) : Query(rson, qp);
return Cmp(tmp, Adv[o], qp) ? Adv[o] : tmp;
} } T; int n, A[N], B[N]; vector<int> G[N]; ll dp[N]; inline void Insert(int ver) {
ll k = B[ver], b = dp[ver] - Lim * k;
T.Insert(rt[ver], 1, Lim * 2, (Line) {k, b, ver});
} inline ll Query(int ver) {
int tmp = T.Query(rt[ver], 1, Lim * 2, A[ver] + Lim).id;
return 1ll * A[ver] * B[tmp] + dp[tmp];
} void Dp(int u, int fa) {
for (int v : G[u]) if (v ^ fa)
Dp(v, u), rt[u] = T.Merge(rt[u], rt[v], 1, Lim * 2);
dp[u] = Query(u); Insert(u);
} int main () {
File(); n = read();
For (i, 1, n) A[i] = read();
For (i, 1, n) B[i] = read(); For (i, 1, n - 1) {
int u = read(), v = read();
G[u].push_back(v); G[v].push_back(u);
} Dp(1, 0); For (i, 1, n) printf ("%lld%c", dp[i], i == iend ? '\n' : ' '); return 0;
}

Codeforces Round #463 F. Escape Through Leaf (李超线段树合并)的更多相关文章

  1. Codeforces 1303G - Sum of Prefix Sums(李超线段树+点分治)

    Codeforces 题面传送门 & 洛谷题面传送门 个人感觉这题称不上毒瘤. 首先看到选一条路径之类的字眼可以轻松想到点分治,也就是我们每次取原树的重心 \(r\) 并将路径分为经过重心和不 ...

  2. Codeforces 1175G - Yet Another Partiton Problem(李超线段树)

    Codeforces 题面传送门 & 洛谷题面传送门 这是一道李超线段树的毒瘤题. 首先我们可以想到一个非常 trivial 的 DP:\(dp_{i,j}\)​ 表示前 \(i\)​ 个数划 ...

  3. [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】

    题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...

  4. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  5. Codeforces 671D Roads in Yusland [树形DP,线段树合并]

    洛谷 Codeforces 这是一个非正解,被正解暴踩,但它还是过了. 思路 首先很容易想到DP. 设\(dp_{x,i}\)表示\(x\)子树全部被覆盖,而且向上恰好延伸到\(dep=i\)的位置, ...

  6. Codeforces 666E Forensic Examination(广义后缀自动机+线段树合并)

    将所有串(包括S)放一块建SAM.对于询问,倍增定位出该子串所在节点,然后要查询的就是该子串在区间内的哪个字符串出现最多.可以线段树合并求出该节点在每个字符串中的出现次数. #include<b ...

  7. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  8. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  9. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

随机推荐

  1. mysql_单表查询

    单表查询 比较符 当在子句中使用 <. >. <=. >=. <>或 !=时,字段的名字不加引号.如果使用引号,则查询出的结果为空,并且不会报错. ; 等号 当在子 ...

  2. Python_字典及其操作

    字典 概念 字典,Python基础数据类型之一,{}以键值对的形式存储数据. 以key : value 形式存储数据.例如,name 为 key,Laonanhai 为 value. dic = {' ...

  3. JEECG SSO kisso

    kisso: java 基于 Cookie 的 SSO 中间件 kisso https://gitee.com/baomidou/kisso kisso首页.文档和下载 - 基于 Cookie 的 S ...

  4. css小demo

    span{ color: #ccc; float: right; font-weight: bold; display: inline-block; border-right: solid 1px # ...

  5. Latex常用

    插入罗马数字 \newcommand{\RNum}[1]{\uppercase\expandafter{\romannumeral #1\relax}} 然后在正文里面就可以用\RNum{}来添加罗马 ...

  6. ELK日志系统+x-pack安全验证

    根据之前已经搭好的ELK系统,现在加一个x-pack插件上去,不然谁拿到ip和端口都可以访问elasticsearch和kibana. 要的效果如下:打开kibana界面的时候要让其输入用户名密码才能 ...

  7. js的日期操作:String转date日期格式、求日期差

    一.在js中String类型转成date格式 var date = new Date("2018-9-21 14:58:43");//就是这么简单 二.date转String类型就 ...

  8. python爬虫之git的安装

      一.初始 1.发展历史 *最开始没有对代码的管理,导致很多东西混乱和丢失. *后来大家想了一个办法,用最简单最笨的方法,各种复制文件夹. *然后就出现了版本控制的工具. 1.单机版:RCS(198 ...

  9. linux中$1的意思

    $1 在shell中成为“位置参数”,表示传入的第一个参数.在shell脚本主体中,表示shell脚本的第一个参数.用在shell脚本函数里时,表示的是函数的第一个入参.

  10. @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

    @EnableAutoConfiguration 作用:Spring Boot会自动根据你jar包的依赖来自动配置项目. 例如当你项目下面有HSQLDB的依赖时,Spring Boot会创建默认的内存 ...