听说正解是啥 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. 【学习总结】Markdown 使用的正确姿势

    参考资料:Learning-Markdown 入门参考 注:原博可能对GitHub中的Markdown格式更适用. 有时间可以再GitHub中尝试并写一篇相关攻略. ps:在王熊猫的GitHub里也有 ...

  2. PAT L3-007 天梯地图

    https://pintia.cn/problem-sets/994805046380707840/problems/994805051153825792 本题要求你实现一个天梯赛专属在线地图,队员输 ...

  3. 17-vue-cli脚手架安装和webpack-simple模板项目生成

    ue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. GitHub地址是:https://github.com/vuejs/vue-cli 一.安 ...

  4. asp.net mvc5 action多个参数

    需要完成http://site.com/user/add/1/2这样的url解析 使用action的参数直接获取数据的方式 Action声明如下 ) { ViewBag.clubID = id; ) ...

  5. Python3练习题 026:求100以内的素数

    p = [i for i in range(2,100)] #建立2-99的列表 for i in range(3,100): #1和2都不用判断,从3开始     for j in range(2, ...

  6. [转帖]HPE的软件部分到底是谁的?

    英国Micro Focus公司收购惠普旗下软件部门 http://www.gongkong.com/news/201710/369740.html 搞不清楚 现在ALM 到底是谁的资产了.. 据国外媒 ...

  7. 在linux和本地系统之间进行数据传输的简单方法--lrzsz

    lrzsz是一款在linux里可代替ftp上传和下载的程序. >>提君博客原创  http://www.cnblogs.com/tijun/  << 提君博客原创 安装和使用非 ...

  8. C# Note22: 《Effective C#》笔记

    参考:<Effective C#>快速笔记(一)- C# 语言习惯 参考:<Effective C#>快速笔记(二)- .NET 资源托管 参考:<Effective C ...

  9. php的amqp扩展 安装(windows) rabbitmq学习篇

    因为RabbitMQ是由erlang语言实现的,所以先要安装erlang环境erlang 下载安装 http://www.erlang.org/download.htmlrabbitmq 下载安装 h ...

  10. dentry path_lookat dput

    https://www.ibm.com/developerworks/cn/linux/l-cn-usagecounter/index.html https://blog.csdn.net/young ...