题目传送门

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. fengmiantu3

  2. 高级软件测试技术(测试管理工具实践day1)

    今天进行了班级内部各小组选择测试工具,选择各自需要进行测试管理工具.我们小组暂定选择 禅道 但是班级内其他小组选择的工具还没确定,还没进行

  3. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_13_使用字节流读取中文的问题

    编码格式右下角显示是UTF-8 前三个字节是你,后三个字节是好.一个汉字占用了三个字节 读一个字节让编程char类型 文件里面后面加上abc abc没有问题 所以java提供字符流.字符流一次读取一个 ...

  4. 《图解设计模式》读书笔记7-1 facade模式

    目录 1. Facade模式简介 2. 示例程序 2.1 类图 2.2 程序 3.角色和类图 4.思路拓展 1. Facade模式简介 开发程序的过程中,随着时间的推移,类会越来越多,调用关系会越来越 ...

  5. Mac 设置git命令tab自动补全(亲测有效)

    转载 :https://blog.csdn.net/tiancaijyy/article/details/84888868 注意: 获取git-completion.bash  要对应自己的git版本 ...

  6. 安装gradle和配置

    1:官网下载地址:https://docs.gradle.org/current/userguide/installation.html 下载自己认为的版本(压缩包) 2:解压到目标目录 3:配置gr ...

  7. EasyUI选项卡避免重复打开

    前台代码: <div data-options="region:'west',title:'我的工作平台',split:true,iconCls:'icon-desk'"  ...

  8. Js基本类型中常用的方法总结

    1.数组 push() -----> 向数组末尾添加新的数组项,参数为要添加的项,返回值是新数组的长度,原数组改变: pop() -----> 删除数组末尾的最后一项,参数无,返回值是删除 ...

  9. vue子组件修改父组件传递过来的值

    这里不再赘述父子组件及子父组件传值,不懂的同学可以翻看我以前写过的关于两者传值的文章 父子组件传值:https://www.cnblogs.com/Sky-Ice/p/9267192.html 子父组 ...

  10. 个人对BFC的见解

    BFC:块级格式化上下文,它是指一个独立的块级渲染区域,只有Block-level BOX参与,该区域拥有一套渲染规则来约束块级盒子的布局,且与区域外部无关. BFC的生成 既然上文提到BFC是一块渲 ...