[nowcoder5669A]Ancient Distance
}_{2}n)$

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的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- python日志配置及调用
0.日志基础操作 import logging logging.basicConfig( #1.日志输出的位置,终端和文件 filename='access.log', #,不指定默认打到终端上 #2 ...
- SpringBoot入门02-配置类
引入 Spring Boot的底层已经有了Spring MVC Spring Boot习惯优先的思想,很多配置都是可省的 不需要配置web.xml文件 不需要服务层的xml配置 不需要dao层的xml ...
- Go语言核心36讲(Go语言进阶技术二)--学习笔记
08 | container包中的那些容器 我们在上次讨论了数组和切片,当我们提到数组的时候,往往会想起链表.那么 Go 语言的链表是什么样的呢? Go 语言的链表实现在标准库的container/l ...
- 力扣 - 剑指 Offer 29. 顺时针打印矩阵
题目 剑指 Offer 29. 顺时针打印矩阵 思路1 其实就是按照理解题目的意思一步步从外层到内层打印出来,同时将一个外层分成四个部分分步打印 可以用一个变量count来维护当前打印的第几层 判断打 ...
- 4.19——数组双指针——26. 删除有序数组中的重复项 & 27. 删除有序数组中的重复项II & 80. 删除有序数组中的重复项 II
第一次做到数组双指针的题目是80: 因为python的List是可以用以下代码来删除元素的: del List[index] 所以当时的我直接用了暴力删除第三个重复元素的做法,大概代码如下: n = ...
- UltraSoft - Beta - Scrum Meeting 1
Date: May 17th, 2020. Scrum 情况汇报 进度情况 组员 负责 今日进度 q2l PM.后端 维护Beta阶段文档 Liuzh 前端 增加删除操作按钮 Kkkk 前端 查询增加 ...
- BUAA软件工程:软件案例分析
BUAA软件工程:软件案例分析 Author:17373015 乔玺华 项目 内容 这个作业属于哪个课程 2020计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 软件案例分析博客作业 我在这个 ...
- WEB前端工程师如何做职业规划?
对于一个WEB前端的职业规划,其实是有各种的答案,没有哪种答案是完全正确的,全凭自己的选择,只要是自己选定了,坚持去认真走,就好.在这里, 我只是 简要说一下自己对于这块儿内容的理解.有一个观点想要分 ...
- Machine learning(2-Linear regression with one variable )
1.Model representation Our Training Set [训练集]: We will start with this ''Housing price prediction'' ...
- populating-next-right-pointers-in-each-node leetcode C++
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...