比赛 |
2025.9.13 |
评测结果 |
AWWEEWWWWWEEEEEEEE |
题目名称 |
Vocabulary Quiz |
最终得分 |
6 |
用户昵称 |
zhyn |
运行时间 |
1.844 s |
代码语言 |
C++ |
内存使用 |
7.86 MiB |
提交时间 |
2025-09-13 11:57:25 |
显示代码纯文本
/*#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 10000005
int n,m=0;
vector<int>e[maxn];
int dep[maxn],siz[maxn];
void dfs(intm u,int fa){
dep[u]=dep[fa]+1;
int p=0;
for(int v:e[u]){
if(v==fa){
continue;
}
dfs(v,u);
p++;
}
siz[u]=max(p,1);
if(siz[u]==0){
m++;
}
}
int main(){
//freopen("Vocabulary.in","r",stdin);
//freopen("Vocabulary.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
e[x].push_back(i);
e[i].push_back(x);
}
dfs(0,maxn+10);
for(int i=1;i<=m;i++){
int x;
cin>>x;
}
return 0;
}*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 100005
int n,m,r,p;
int cnt=0;
int a[maxn],f[maxn],sz[maxn],top[maxn],dep[maxn],son[maxn],id[maxn],rec[maxn],siz[maxn];
ll w[maxn*4],lzy[maxn*4],ans;
vector<int>e[maxn];
void dfs1(int u,int fa){
sz[u]=1;
siz[u]=1;
dep[u]=dep[fa]+1;
f[u]=fa;
int p=0;
for(int v:e[u]){
if(v==fa){
continue;
}
dfs1(v,u);
siz[u]++;
sz[u]+=sz[v];
if(sz[son[u]]<sz[v]){
son[u]=v;
}
}
if(siz[u]==1){
m++;
}
}
void dfs2(int u,int t){
top[u]=t;
id[u]=++cnt;
rec[cnt]=u;
if(!son[u]){
return;
}
dfs2(son[u],t);
for(int v:e[u]){
if(v==f[u]||v==son[u]){
continue;
}
dfs2(v,v);
}
}
void pushup(int u){
w[u]=w[u*2]+w[u*2+1];
}
void tags(int u,int len,int z){
lzy[u]+=z;
w[u]+=len;
}
void pushdown(int u,int l,int r){
int mid=(l+r)/2;
tags(u*2,mid-l+1,lzy[u]);
tags(u*2+1,r-mid,lzy[u]);
lzy[u]=0;
}
void build(int u,int l,int r){
if(l==r){
w[u]=a[rec[l]];
return;
}
int mid=(l+r)/2;
build(u*2,l,mid),build(u*2+1,mid+1,r);
pushup(u);
}
void update(int u,int l,int r,int ql,int qr,ll z){
if(l>=ql&&r<=qr){
tags(u,r-l+1,z);
}
else if(!(l>qr||r<ql)){
int mid=(l+r)/2;
pushdown(u,l,r);
update(u*2,l,mid,ql,qr,z);
update(u*2+1,mid+1,r,ql,qr,z);
pushup(u);
}
}
ll query(int u,int l,int r,int ql,int qr){
if(l>=ql&&r<=qr){
return w[u];
}
else if(!(l>qr||r<ql)){
int mid=(l+r)/2;
pushdown(u,l,r);
return query(u*2,l,mid,ql,qr)+query(u*2+1,mid+1,r,ql,qr);
}
else return 0;
}
void upd(int l,int r,int z){
while(top[l]!=top[r]){
if(dep[top[l]]<dep[top[r]]){
swap(l,r);
}
update(1,1,n,id[top[l]],id[l],z);
l=f[top[l]];
}
update(1,1,n,min(id[l],id[r]),max(id[l],id[r]),z);
}
ll qry(int l,int r){
ll ans=0;
while(top[l]!=top[r]){
if(dep[top[l]]<dep[top[r]]){
swap(l,r);
}
ans+=query(1,1,n,id[top[l]],id[l]);
l=f[top[l]];
}
return ans+query(1,1,n,min(id[l],id[r]),max(id[l],id[r]));
}
void query1(int u){
if(f[u]==0){
return;
}
siz[u]--;
if(siz[u]==1){
ans=dep[u];
query1(f[u]);
}
}
int main(){
freopen("Vocabulary.in","r",stdin);
freopen("Vocabulary.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
a[i]=1;
}
for(int i=1;i<=n;i++){
int u;
cin>>u;
u++;
e[i+1].push_back(u);
e[u].push_back(i+1);
}
dfs1(1,0);
dfs2(1,0);
//好像没法用树剖,线段树会炸掉
for(int i=1;i<=m;i++){
int x;
cin>>x;
ans=dep[x];
query1(x);
cout<<ans-2<<"\n";
}
return 0;
}