题解 Lost My Music
多明显的斜率式然而我没有看出来
然而不管是我乱搞的思路还是正解的凸包思路都需要一个可持久化栈
考场上想到可持久化单调栈,但不会实现……
其实单调栈不管是否可持久化都能倍增弹栈
但普通单调栈本来就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的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
- JSOI2016R3 瞎BB题解
题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...
随机推荐
- 将Acunetix与CircleCI集成
如果要在DevSecOps中包含Acunetix ,则需要将其与CI / CD系统集成.Acunetix具有针对最受欢迎的CI / CD系统Jenkins的现成集成.但是,您可以使用Acunetix ...
- C++ 继承及委托
从内存角度看继承和多重继承 http://www.doc88.com/p-9075148832569.html 在C++中实现委托(Delegate) https://blog.csdn.net/jf ...
- python使用笔记11--时间模块
1.时间模块常用方法 1 import time,datetime 2 #格式化好的时间2020-05-16 18:30:52 3 #时间戳1589616753 从unix元年(计算机发明的时间)到现 ...
- C语言:赋值流程图
- Python+Requests+Xpath(解析)爬取某站点简历图片(数据分析三)
1.环境安装 pip install lxml 2.解析原理 使用通用爬虫爬取网页数据 实例化etree对象,且将页面数据加载到该对象中 使用xpath函数结合xpath表达式进行标签定位和指定数据提 ...
- python -- 面向对象编程(类、对象)
一.类 类是用来描述具有相同的属性和方法的对象的集合. 它定义了该集合中每个对象共同拥有的属性和方法. 类是一个独立的单位,它有一个类名,其内部包括成员变量和成员方法,分别用于描述对象的属性和行为. ...
- Cent OS 7 本地yum源配置与安装
一.本地yum源 1.添加一个新的yum源配置文件dvd.repo(文件名字自定义) vi etc/yum.repos.d 添加新的内容: name=rhel_dvd ...
- 搭建SAMBA服务
说明:这里是Linux服务综合搭建文章的一部分,本文可以作为单独搭建SABMA服务的参考. 注意:这里所有的标题都是根据主要的文章(Linux基础服务搭建综合)的顺序来做的. 如果需要查看相关软件版本 ...
- spring boot+mybatis plus出现Invalid bound statement (not found)
qlSessionFactory不要使用原生的,请使用MybatisSqlSessionFactory
- 基于单机redis的分布式锁实现
最近我们有个服务经常出现存储的数据出现重复,首先上一个系统流程图: 用户通过http请求可以通知任务中心结束掉自己发送的任务,这时候任务中心会通过MQ通知结束服务去结束任务保存数据,由于任务结束数据计 ...