听说正解是啥 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. 多线程系列之二:Single Thread Execution 模式

    一,什么是SingleThreadExecution模式?同一时间内只能让一个线程执行处理 二,例子 1.不安全的情况 用程序模拟 三个人频繁地通过一个只允许一个人经过的门.当人通过时,统计人数便会增 ...

  2. semantic-ui 标题

    在semantic-ui中定义了5中标题样式,注意HTML中有h1-h6,而semantic-ui中只有h1-h5. 不过需要注意的是,semantic-ui的标题仍旧使用h1-h5来表示,但是在cl ...

  3. 使用node操作mongodb

    let mongodb = require('mongodb'); let MongodbClient = mongodb.MongoClient; MongodbClient.connect('mo ...

  4. [转帖]你云我云•兄弟夜谈会 第三季 企业IT架构

    你云我云•兄弟夜谈会 第三季 企业IT架构 https://www.cnblogs.com/sammyliu/p/10425252.html 你云我云•兄弟夜谈会 第三季 企业IT架构 你云我云•兄弟 ...

  5. Angular 自定义过滤器

    <!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...

  6. python爬虫之git的使用(windows下pycharm使用)

    相信很多同学学会了git或者github以后都不知道怎么跟windows上的pycharm连在一起工作,那么下面我们开始介绍简单的安装和使用方法. 一.安装 1.首先你的有一个github的账户.注册 ...

  7. 我和我的小伙伴们都惊呆了!基于Canvas的第三方库Three.js

    What is Three.js three + js 表示运行在浏览器上的3D程序 javascript的计算能力因为google的V8引擎得到了迅猛提升 做服务器都没问题了 更别说3D了 哈哈  ...

  8. Python时间的简单使用

    1.time.strptime(string[, format]),string -- 时间字符串.format -- 格式化字符串.返回struct_time对象.     把字符串转换为时间格式, ...

  9. python RSA 加密与签名

    PyCrypto装起来就简单多了,我是直接 sudo easy_install pycrypto 直接搞定的 先生成rsa的公私钥:打开控制台,输入 openssl 再输入 genrsa -out p ...

  10. Windows上安装 TensorFlow及简单命令

    1.官网及帮助文档 官网: https://www.tensorflow.org/install/install_windows 中文帮助文档:https://efeiefei.gitbooks.io ...