题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2402

题解

看上去很像分数规划的模型。于是就二分吧。令

\[\begin{align*}\frac{y_i+q_j}{x_i+p_j} &\geq mid\\y_i+q_j &\geq mid(x_i+p_j)\\(y_i - mid\cdot x_i) + (q_j - mid\cdot p_j) & \geq 0\end{align*}
\]

这样 \(x, y\) 和 \(p, q\) 两组量就毫不相关了。下面就以 \(x, y\) 这一组量为例。


现在的问题是求出树上 \(A\) 到 \(B\) 的路径上的 \(y_i - mid \cdot x_i\) 的最大值。

容易发现,如果我们令 \(y_i - mid \cdot x_i\) 是关于 \(mid\) 的一次函数,在坐标系上可以表示一条以 \(mid\) 为 \(x\) 轴的直线。如果我们可以得到从 \(A\) 到 \(B\) 的路径上的每一个点的 \(x\) 和 \(y\),那么可以得到很多的直线。我们需要的是很多条直线在 \(mid\) 处的最大值。

考虑只保留对于任意一个 \(mid\) 时取最大值的坐标点,那么这应该是一个下凸壳。如果我们可以维护出这个下凸壳的话,对于一个已知的 \(mid\),只需要在凸壳上二分它是在哪一条直线上就可以求出来了。


现在考虑如果得到这个下凸壳。可以使用树剖,对于线段树上每一段,维护这个区间的凸壳。

维护方法就是直接对于每一段暴力建凸壳就行了,反正总长度为 \(n\log n\) 级别。


于是总的时间复杂度为 \(O(n\log n +q \log ^4n)\)。后面的 \(\log^4n\) 分别来自分数规划的二分、树链剖分、线段树、凸壳上二分。

由于本题时限充足,以及中间的两个 \(\log n\) 都跑的不是很满,所以可以通过此题。


本题细节比较多,可能不是很好调,写的时候要注意一些细节,比如 vector 的第一位的下标为 \(0\),小心越界、注意特判掉 \(k\) 值相同的直线等等。


#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} #define lc o << 1
#define rc o << 1 | 1 const int N = 30000 + 7; int n, m, dfc;
double maxy, maxq;
int dep[N], f[N], siz[N], son[N], dfn[N], pre[N], top[N]; struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); } struct Line {
double k, b;
inline Line() {}
inline Line(const double &k, const double &b) : k(k), b(b) {}
inline bool operator < (const Line &a) const { return k < a.k || (k == a.k && b > a.b); }
inline double get_y(const double &x) { return k * x + b; }
} tmp[N], tmp2[N]; inline double get_x(const Line &a, const Line &b) { return (b.b - a.b) / (a.k - b.k); } struct SGT {
Line a[N];
std::vector<Line> t[N << 2]; inline void calc(int o, int L, int R) {
int M = (L + R) >> 1;
std::merge(tmp + L, tmp + M + 1, tmp + M + 1, tmp + R + 1, tmp2 + 1);
std::copy(tmp2 + 1, tmp2 + R - L + 2, tmp + L);
int tl = 0;
t[o].push_back(tmp[L]);
for (int i = L + 1; i <= R; ++i) {
while (tl >= 1 && get_x(tmp[i], t[o][tl - 1]) <= get_x(t[o][tl], t[o][tl - 1])) --tl, t[o].pop_back();
t[o].push_back(tmp[i]), ++tl;
}
}
inline double get_ans(int o, double x) {
if (t[o].size() == 1) return t[o][0].get_y(x);
int l = 1, r = t[o].size() - 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (get_x(t[o][mid - 1], t[o][mid]) <= x) l = mid;
else r = mid - 1;
}
if (get_x(t[o][l - 1], t[o][l]) <= x) return t[o][l].get_y(x);
else return t[o][l - 1].get_y(x);
}
inline void build(int o, int L, int R) {
if (L == R) return tmp[L] = a[pre[L]], t[o].pb(tmp[L]), (void)0;
int M = (L + R) >> 1;
build(lc, L, M), build(rc, M + 1, R);
calc(o, L, R);
}
inline double qmax(int o, int L, int R, int l, int r, double x) {
if (l <= L && R <= r) return get_ans(o, x);
int M = (L + R) >> 1;
if (r <= M) return qmax(lc, L, M, l, r, x);
if (l > M) return qmax(rc, M + 1, R, l, r, x);
return std::max(qmax(lc, L, M, l, r, x), qmax(rc, M + 1, R, l, r, x));
}
} A, B; inline void dfs1(int x, int fa = 0) {
dep[x] = dep[fa] + 1, f[x] = fa, siz[x] = 1;
for fec(i, x, y) if (y != fa) dfs1(y, x), siz[x] += siz[y], siz[y] > siz[son[x]] && (son[x] = y);
}
inline void dfs2(int x, int pa) {
top[x] = pa, dfn[x] = ++dfc, pre[dfc] = x;
if (!son[x]) return; dfs2(son[x], pa);
for fec(i, x, y) if (y != f[x] && y != son[x]) dfs2(y, y);
} inline double qmax(int x, int y, double k) {
double ans1 = -1e9, ans2 = -1e9;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
smax(ans1, A.qmax(1, 1, n, dfn[top[x]], dfn[x], k));
smax(ans2, B.qmax(1, 1, n, dfn[top[x]], dfn[x], k));
x = f[top[x]];
}
if (dep[x] > dep[y]) std::swap(x, y);
smax(ans1, A.qmax(1, 1, n, dfn[x], dfn[y], k));
smax(ans2, B.qmax(1, 1, n, dfn[x], dfn[y], k));
return ans1 + ans2;
}
inline double solve(int x, int y) {
double l = 0, r = maxy + maxq;
while (r - l > 0.001) {
double mid = (l + r) / 2;
if (qmax(x, y, mid) >= 0) l = mid;
else r = mid;
}
return l;
} inline void work() {
dfs1(1), dfs2(1, 1), A.build(1, 1, n), B.build(1, 1, n);
int m;
read(m);
while (m--) {
int x, y;
read(x), read(y);
printf("%.4lf\n", solve(x, y));
}
} inline void init() {
read(n);
for (int i = 1; i <= n; ++i) scanf("%lf", &A.a[i].k), A.a[i].k = -A.a[i].k;
for (int i = 1; i <= n; ++i) scanf("%lf", &A.a[i].b), smax(maxy, A.a[i].b);
for (int i = 1; i <= n; ++i) scanf("%lf", &B.a[i].k), B.a[i].k = -B.a[i].k;
for (int i = 1; i <= n; ++i) scanf("%lf", &B.a[i].b), smax(maxq, B.a[i].b);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), adde(x, y);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj2402 陶陶的难题II 分数规划+树剖+线段树维护凸壳+二分的更多相关文章

  1. 【BZOJ2402】陶陶的难题II 分数规划+树链剖分+线段树+凸包

    题解: 首先分数规划是很明显的 然后在于我们如何要快速要求yi-mid*xi的最值 这个是看了题解之后才知道的 这个是斜率的一个基本方法 我们设y=mid*x+z 那么显然我们可以把(x,y)插入到一 ...

  2. 【bzoj2402】陶陶的难题II 分数规划+树链剖分+线段树+STL-vector+凸包+二分

    题目描述 输入 第一行包含一个正整数N,表示树中结点的个数.第二行包含N个正实数,第i个数表示xi (1<=xi<=10^5).第三行包含N个正实数,第i个数表示yi (1<=yi& ...

  3. BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)

    题目大意:略 一定范围内求最大值,考虑二分答案 设现在选择的答案是$mid$,$max \left \{ \frac{yi+qj}{xi+pj} \right \} \geq mid $ 展开可得,$ ...

  4. 洛谷2543AHOI2005]航线规划 (树剖+线段树+割边思路)

    这个题的思路还是比较巧妙的. 首先,我们发现操作只有删除和询问两种,而删除并不好维护连通性和割边之类的信息. 所以我们不妨像WC2006水管局长那样,将询问离线,然后把操作转化成加边和询问. 然后,我 ...

  5. BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)

    前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...

  6. bzoj 2402: 陶陶的难题II 二分答案维护凸包

    2402: 陶陶的难题II Time Limit: 40 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 68  Solved: 45[Submi ...

  7. 【转】[Algorithm]01分数规划

    因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...

  8. BZOJ1758: [Wc2010]重建计划(01分数规划+点分治+单调队列)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1758 01分数规划,所以我们对每个重心进行二分.于是问题转化为Σw[e]-mid>=0, ...

  9. 【BZOJ5281】Talent Show(分数规划)

    [BZOJ5281]Talent Show(分数规划) 题面 BZOJ 洛谷 题解 二分答案直接就是裸的分数规划,直接跑背包判断是否可行即可. #include<iostream> #in ...

随机推荐

  1. 图片上传预览转压缩并转base64详解(dShowImg64.js)

    hello,大家好,游戏开始了,欢迎大家收看这一期的讲解.本次的内容是图片的上传预览.最后发源码链接.废话不多说,先上图. 待上传图像 点击蓝色框内,pc可以选择文件,移动端选择拍照或选择图片进行上传 ...

  2. java中 使用输入+输出流对对象序列化

    对象: 注意记得实现 Serializable package com.nf147.sim.entity; import java.io.Serializable; public class News ...

  3. selinux的设置包括两个部分: 修改安全上下文和修改策略

    selinux的设置包括两个部分: 修改安全上下文和修改策略 修改安全上下文: chcon = change context: chcon 修改策略: setsebool 策略就是由很多boo类型的变 ...

  4. leetcode 39. 组合总和(python)

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  5. MongoDB学习【二】—MongoDB基础和数据类型

    一.MongoDB基础知识 在MongoDB中相关术语的解释和sql术语对应关系 SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table col ...

  6. JS 弹出网页 (不显示地址栏,工具栏) 网页去掉地址栏

    JS 弹出网页 (不显示地址栏,工具栏) 网页去掉地址栏 window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 基本语法: ...

  7. Delphi XE2 之 FireMonkey 入门(6) - TLine、TEllipse、TCircle、TPie、TArc、TRectangle、TRoundRect、TCalloutRectangle

    它们都是继承自 TShape 类, 共同拥有如下属性: Fill            : TBrush;      //填充 Stroke          : TBrush;      //边线( ...

  8. 《Python Data Structures》 Week4 List 课堂笔记

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week4 List 8.2 Manipulating Lists 8 ...

  9. usb接口类型 简单分类辨识

    usb接口类型 简单分类辨识 - [相似百科] 庆欣 0.0 4 人赞同了该文章 1. 先放图,随着越来越多的接触智能设备,会遇到各种各样的usb接口,对于很多人来说,接口类型只有:usb接口,安卓接 ...

  10. 【SD系列】SAP SD模块-公司间销售简介

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP SD模块-公司间销售简介   ...