传送门

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

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

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

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

但普通单调栈本来就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. Servlet核心技术

    一.基本概念 1.C/S C/S架构是客户端服务器架构,将需要处理的业务合理的分配到客户端和服务器,客户端负责与用户的交互任务,服务器负责数据管理. 优点: 客户端界面和功能可以很丰富 应用服务器负荷 ...

  2. python使用笔记26--多线程、多进程

    1.概念 线程.进程 进程是资源的集合,也就是一个程序 线程是一个程序运行的最小单位 线程是在进程里面的 默认,一个进程就只有一个线程 一个电脑有几核CPU就只能同时运行几个任务,比如4核CPU只能同 ...

  3. LeetCode 982. Triples with Bitwise AND Equal To Zero

    题目链接:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/ 题意,已知数组A,长度不超过1000,最大的数不超 ...

  4. HelloWord!

    HelloWorld 1.新建一个文件夹,存放代码 2.新建一个java文件后缀为.java Hello.java (注意后缀 如系统没打开显示后缀需要打开) 3.编写HelloWorld代码 (建议 ...

  5. 微信小程序云开发-数据条件查询

    一.使用where条件查询 在.get()语句之前增加.where语句实现条件查询.  二.通过doc查询单条数据 1.使用doc来查询数据库中的单条数据 2.定义一个空对象,用来展示插叙到的单条数据 ...

  6. CF1214E Petya and Construction Set题解

    原来这就叫构造题,了 这道题的做法,我自己诌了一个形象的名字--"挂葡萄"法( 首先,"搭葡萄架":考虑到每个距离 \(d_i\) 只与 \(2i-1,2i\) ...

  7. 【动态规划】合唱队形 luogu-

    分析 做两遍最长上升子序列,在遍历一下,取最大值. AC代码 #include <bits/stdc++.h> using namespace std; #define ms(a,b) m ...

  8. C++第三十七篇 -- 调试驱动程序

    上一篇写的KMDF程序是通过串口进行配置的,那么我们在VS中Attach to process外,可以直接用Winbdg进行调试,winbdg.exe所在路径为C:\Program Files (x8 ...

  9. Codeforces Round #735 (Div. 2) 题解

    比赛地址:https://codeforces.com/contest/1554. 只有 ABCD 的题解,E 不会. A 答案是 \(\max_i\{a_ia_{i+1}\}\).证明:(反证)如果 ...

  10. 5G时代,视频会议的未来

    过去,2G打开了了移动互联网天下,3G促成了即时通信,诞生了QQ.微信等巨头,4G 带来了短视频兴起,字节跳动等公司崛起.2.3.4G的出现促成了移动互联网10年繁荣.而5G的出现,也会促成至少10年 ...