对于一个$k$,可以二分枚举答案并判断,判断过程可以贪心找最深的点(线段树区间max)+倍增+线段树区间覆盖(清0)来实现,时间复杂度$o(klog_{2}n)$
考虑反过来,暴力枚举答案$x$并求出最少需要的点数量$f(x)=k$,那么$\forall \ f(x)\le i< f(x-1)$,都有$i$的答案为$x$
这样的复杂度看似仍然是$o(n^{2}log_{2}n)$,但发现一次贪心至少覆盖$x+1$个点或者已经是最后一次,也就是说最多说单次复杂度为$o(\lceil \frac{n}{x+1}\rceil log_{2}n)$,累加后通过调和级数保证复杂度为$o(nlog^{\ 2
}_{2}n)$
具体线段树的实现上可能比较难,可以再维护一个$tag[k]$表示,可能标记永久化+重新修改来实现会比较方便
另外常数较大,注意线段树常数

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define L (k<<1)
5 #define R (L+1)
6 #define mid (l+r>>1)
7 struct ji{
8 int nex,to;
9 }edge[N];
10 vector<int>v;
11 int E,n,x,head[N],dfn[N],sz[N],sh[N],tr[N<<2],tr2[N<<2],tag[N<<2],f[N][21];
12 void add(int x,int y){
13 edge[E].nex=head[x];
14 edge[E].to=y;
15 head[x]=E++;
16 }
17 void build(int k,int l,int r){
18 tag[k]=1;
19 if (l==r){
20 tr[k]=tr2[k]=sh[l];
21 return;
22 }
23 build(L,l,mid);
24 build(R,mid+1,r);
25 tr[k]=tr2[k]=max(tr[L],tr[R]);
26 }
27 void update(int k,int l,int r,int x,int y,int z){
28 if ((l>y)||(x>r))return;
29 if ((x<=l)&&(r<=y)){
30 tag[k]=z;
31 tr[k]=tr2[k]*z;
32 return;
33 }
34 update(L,l,mid,x,y,z);
35 update(R,mid+1,r,x,y,z);
36 tr[k]=max(tr[L],tr[R])*tag[k];
37 }
38 int query(int k,int l,int r){
39 if (l==r)return l;
40 if (tr[k]==tr[L])return query(L,l,mid);
41 return query(R,mid+1,r);
42 }
43 void dfs(int k,int fa,int s){
44 sz[k]=1;
45 sh[k]=s;
46 dfn[k]=++x;
47 f[k][0]=fa;
48 for(int i=1;i<=20;i++)f[k][i]=f[f[k][i-1]][i-1];
49 for(int i=head[k];i!=-1;i=edge[i].nex){
50 dfs(edge[i].to,k,s+1);
51 sz[k]+=sz[edge[i].to];
52 }
53 }
54 int find(int k,int x){
55 for(int i=0;i<=20;i++)
56 if (x&(1<<i))k=f[k][i];
57 return k;
58 }
59 int main(){
60 while (scanf("%d",&n)!=EOF){
61 E=0;
62 for(int i=1;i<=n;i++)head[i]=-1;
63 for(int i=2;i<=n;i++){
64 scanf("%d",&x);
65 add(x,i);
66 }
67 x=0;
68 dfs(1,1,1);
69 build(1,1,n);
70 int ans=-1;
71 for(int i=1;i<=n;i++){
72 v.clear();
73 while (1){
74 x=find(query(1,1,n),i);
75 if (x==1)break;
76 v.push_back(x);
77 update(1,1,n,dfn[x],dfn[x]+sz[x]-1,0);
78 }
79 for(int j=0;j<v.size();j++)update(1,1,n,dfn[v[j]],dfn[v[j]]+sz[v[j]]-1,1);
80 ans+=v.size()+1;
81 }
82 printf("%d\n",ans);
83 }
84 }

[nowcoder5669A]Ancient Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  4. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  5. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  9. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. 每日总结:Java基本语法 (2021.9.23)

         对象:对象是类的一个实例,有状态和行为. 类:类是一个模板,它描述一类对象的行为和状态. 方法:方法就是行为,一个类可以有很多方法. 实例变量:每个对象都有独特的实例变量,对象的状态由这些实 ...

  2. 题解 「SDOI2017」硬币游戏

    题目传送门 Description 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数多谁胜利. 大家纷纷觉得这个游戏非常符合同学们的特色,但只是扔硬币实在是太单调了. 同学们觉得要加强 ...

  3. bzoj4821 && luogu3707 SDOI2017相关分析(线段树,数学)

    题目大意 给定n个元素的数列,每一个元素有x和y两种元素,现在有三种操作: \(1\ L\ R\) 设\(xx\)为\([l,r]\)的元素的\(x_i\)的平均值,\(yy\)同理 求 \(\fra ...

  4. Netty 了解

    1.1 Netty 是什么? Netty is an asynchronous event-driven network application framework for rapid develop ...

  5. Java(30)集合五Set

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228440.html 博客主页:https://www.cnblogs.com/testero ...

  6. DL4J实战之五:矩阵操作基本功

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. Spark解决SQL和RDDjoin结果不一致问题(工作实录)

    问题描述:DataFrame的join结果不正确,dataframeA(6000无重复条数据) join dataframeB(220条无重复数据,由dataframeA转化而来,key值均源于dat ...

  8. BUAA 2020 软件工程 热身作业

    BUAA 2020 软件工程 热身作业 Author: 17373051 郭骏 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 第一次作业-热身! ...

  9. 虚树 virtual-tree

    我们发现,如果一棵树中真正需要处理的点很少,而总共点数很多时,可以只处理那些需要的点,而忽略其他点. 因此我们可以根据那些需要的点构建虚树,只保留关键点. oi-wiki上对虚树的介绍 我们根据一下方 ...

  10. tcp 三次握手建立连接难点总结

    所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包. 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方的序列号和确认号 ...