题目链接

Implementation

    int n, q; scan(n,q);

    vi p(n + 1);
vi nson(n + 1); up (i, 1, n) {
scan(p[i]);
nson[p[i]]++;
} vi leaf;
up (i, 1, n) {
if (nson[i] == 0) leaf.pb(i);
} vi cnt;
cnt.pb(n); for (; !leaf.empty(); ) {
cnt.pb(cnt.back() - SZ(leaf));
vi tmp;
FOR (x, leaf) {
--nson[p[x]];
if (nson[p[x]] == 0 && p[x] != 0) {
tmp.pb(p[x]);
}
}
leaf = std::move(tmp);
} rep (q) {
int x; scan(x); // 小于等于 x 的第一个数的下标
auto i = lower_bound(all(cnt), x, greater<int>()) - cnt.begin(); debug(i);
if (i == SZ(cnt)) {
println(SZ(cnt));
}
else if (i == 0) {
println(i + 1);
}
else {
int d1 = x - cnt[i];
int d2 = cnt[i - 1] - x;
println(d2 <= d1 ? i : i + 1);
}
}

Note

这道题的实现要点是如何维护每天的叶子集合。

key observation: 对于 $i \ge 3$,第 $i$ 天掉落的叶子一定是某些第 $i - 1$ 天掉落的叶子的父亲。

对应的代码是

        vi tmp;
FOR (x, leaf) {
--nson[p[x]];
if (nson[p[x]] == 0 && p[x] != 0) {
tmp.pb(p[x]);
}
}
leaf = std::move(tmp);

最新文章

  1. Windows应用程序快捷方式创建工具
  2. MySQL------Navicat激活方法
  3. ubuntu科学计算包blas和lapack的安装
  4. E - Power Strings,求最小周期串
  5. I/O 流---输出流
  6. 4G通信技术LTE介绍
  7. Android中AsyncTask的简单用法 .
  8. [翻译]在Django项目中添加谷歌统计(Google Analytics)
  9. ReactiveCocoa &amp; FRP &amp; MVVM
  10. java为移动端写接口
  11. 关于统一资源标志符URL的理解
  12. Java重定向和转发的路径问题
  13. 百度地图JS调用示例
  14. Less的Extend_Less继承
  15. Springboot Selenide UI 自动化测试
  16. 关于mysql 表导入数据
  17. yii2记录
  18. Linux stress CPU的测试方法
  19. 廖雪峰Java2面向对象编程-5包和classpath-1静态字段和方法
  20. About Saliency Object Detection

热门文章

  1. 深入理解LINUX下动态库链接器/加载器ld-linux.so.2
  2. 筛选前十按a-z顺序排
  3. $\LaTeX$数学公式大全6
  4. Inter IPP+ VS + opencv 在 Windows下的环境搭建
  5. 12 Linux ACL权限
  6. lockfree buffer test
  7. ul 加 li 实现 select 下拉选功能
  8. 一、基础篇--1.2Java集合-HashMap源码解析
  9. RF框架自定义测试库开发
  10. 堆的ptmalloc机制