传送门

多明显的斜率式然而我没有看出来

然而不管是我乱搞的思路还是正解的凸包思路都需要一个可持久化栈

考场上想到可持久化单调栈,但不会实现……

其实单调栈不管是否可持久化都能倍增弹栈

但普通单调栈本来就O(n)的倍增弹栈也没啥用

  • 可持久化单调栈/队列注意要倍增处理

    这里涉及求凸包切线

    如果是个普通单调栈维护的凸包直接三分求就行

    但这里的可持久化单调栈是倍增维护的,不好三分

    所以考虑如何判断当前枚举到的点在最终答案点的哪一侧

    如果我们要check一个点v,发现在答案左侧有calc(u, fa(v))>calc(u, v),而答案右侧有calc(u, fa(v))<calc(u, v)

    就可以二分了 其实普通单调栈也可以这样避免三分

    完全想不到……

Code:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1000100
#define ll long long
#define ld long double
#define usd unsigned
#define ull unsigned long long
#define double long double
//#define int long long #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
char buf[1<<21], *p1=buf, *p2=buf;
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
} int n;
int head[N], size;
double c[N];
struct edge{int to, next;}e[N<<1];
inline void add(int s, int t) {edge* k=&e[++size]; k->to=t; k->next=head[s]; head[s]=size;} namespace force{
double ans[N];
void dfs(int u, double cv, int dis) {
if (dis) ans[u] = min(ans[u], (cv-c[u])/(1.0*dis));
for (int i=head[u]; i; i=e[i].next) dfs(e[i].to, cv, dis+1);
}
void solve() {
for (int i=1; i<=n; ++i) ans[i]=1e30;
for (int i=1; i<=n; ++i) dfs(i, c[i], 0);
for (int i=2; i<=n; ++i) printf("%.10Lf\n", ans[i]);
exit(0);
}
} namespace task{
//struct que{double c, d, k; inline void build(double c_, double d_, double k_){c=c_; d=d_; k=k_;}}sta[N][25];
int sta[N][25];
double ans[N], dep[N], reck[N];
inline double calc(int u, int v) {return (c[u]-c[v])/(dep[u]-dep[v]);}
void dfs(int u) {
//cout<<"dfs "<<u<<endl;
//cout<<"P: "<<dep[u]<<' '<<c[u]<<endl;
for (int i=1; i<=22; ++i)
if (dep[u]>=(1<<i)) sta[u][i]=sta[sta[u][i-1]][i-1];
else break;
//cout<<"sta: "; for (int t=sta[u][0]; t; t=sta[t][0]) cout<<t<<' '; cout<<endl;
if (u!=1) {
int now=u;
for (int i=22; i>=0; --i)
if (sta[sta[now][i]][0] && calc(u, sta[now][i])<calc(u, sta[sta[now][i]][0])) now=sta[now][i]; // cout<<"now: "<<now<<endl;
//while (sta[now][0] && calc(u, sta[now][0])>=k) {k=calc(u, sta[now][0]); now=sta[now][0];} //cout<<"now: "<<now<<' '<<k<<endl;
if (sta[now][0]) now=sta[now][0];
ans[u]=calc(u, now);
//cout<<"choose: "<<now<<endl;
sta[u][0]=now;
}
for (int i=1; i<=22; ++i)
if (dep[u]>=(1<<i)) sta[u][i]=sta[sta[u][i-1]][i-1];
else break;
for (int i=head[u],v; i; i=e[i].next) {
v = e[i].to;
dep[v]=dep[u]+1; sta[v][0]=u;
dfs(v);
}
//cout<<"return "<<endl;
}
void solve() {
dep[1]=1;
dfs(1);
for (int i=2; i<=n; ++i) printf("%.10Lf\n", -1.0*ans[i]);
exit(0);
}
} signed main()
{
#ifdef DEBUG
freopen("1.in", "r", stdin);
#endif n=read();
for (int i=1; i<=n; ++i) c[i]=read();
for (int i=2; i<=n; ++i) add(read(), i);
//force::solve();
task::solve(); return 0;
}

题解 Lost My Music的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

  10. JSOI2016R3 瞎BB题解

    题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...

随机推荐

  1. LevelDB学习笔记 (3): 长文解析memtable、跳表和内存池Arena

    LevelDB学习笔记 (3): 长文解析memtable.跳表和内存池Arena 1. MemTable的基本信息 我们前面说过leveldb的所有数据都会先写入memtable中,在leveldb ...

  2. 中国剩余定理简析(python实现)

    中国剩余定理CRT 正整数m1,m2,...,mk两两互素,对b1,b2,...,bk的同余式组为 \[\begin{cases} x \equiv b_1\; mod \;m_1\\ x \equi ...

  3. web自动化之浏览器启动

    一.环境准备 1.本地引入jar 从http://selenium-release.storage.googleapis.com/index.html?path=3.9/,下载selenium-ser ...

  4. Java基础00-多线程28

    1. 实现多线程 1.1 进程 1.2 线程 1.3 多线程的实现方式(方式一:继承Thread类) 代码示例:定义类MyThread: //1:定义一个类MyThread继承Thread类 publ ...

  5. [刘阳Java]_酷炫视频播放器制作_界面篇

    今天开始分享一篇酷炫播放器制作,包括界面+JS.整个案例非常类似腾讯视频,优酷视频,爱奇艺视频.我们先看一下效果图,然后这篇文章主要界面篇 是不是效果比较酷炫,那么我接着来给大家说一下这个界面设计思路 ...

  6. 牛客OI测试赛1

    题目链接: https://www.nowcoder.com/acm/contest/181#question A.斐波拉契 求$f[n-1]*f[n+1]-f[n]^2$,$f[n]$为斐波拉契数列 ...

  7. 安装react后运行报错

    错误提示:npm WARN checkPermissions Missing write access to C:\Users\LXD\Desktop\webpack-base\node_module ...

  8. PAT乙级:1061 判断题 (15分)

    PAT乙级:1061 判断题 (15分) 题干 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 ...

  9. LCT(Link-Cut-Tree)

    LCT(Link-Cut-Tree) LCT维护一个森林,即把每个节点用splay维护,可以进行许多操作: 查询.修改链上的信息 随意指定原树的根(即换根) 动态连边.删边 合并两棵树.分离一棵树 动 ...

  10. SSM和Maven整合

    项目架构如图 applicationContext.xml还有其他文件一起放进resources下 jsp,js,等文件放在webapp下