比赛 4043级2023省选练习赛1 评测结果 AAAAAAAAAA
题目名称 树的果实 最终得分 100
用户昵称 op_组撒头屯 运行时间 0.515 s
代码语言 C++ 内存使用 9.28 MiB
提交时间 2023-03-03 19:58:42
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define si set<int>
#define unsi unordered_set<int>
#define qi queue<int>
#define sti stack<int>
#define pqi priority_queue<int>
#define mii map<int,int>
#define unmii unordered_map<int,int>
#define fi first
#define se second
#define pb push_back
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
const int N=100000+5;
const int M=100000+5;
struct sdf{
    int to,next;
}eg[2*N];
int head[N],ne=0;
void add(int x,int y){
    eg[++ne]={y,head[x]};
    head[x]=ne;return ;
} 
int n,a[N],b[N],m=0,num[M];
int l1[M],l2[M];
int ans[N][2];
int lowbit(int x){
    return x&(-x);
}
void add(int *len,int x,int y){
    for (int i=x;i<=m;i+=lowbit(i))len[i]+=y;
}
int query(int *len,int x){
    if (!x)return 0;int t=0;
    for (int i=x;i>0;i-=lowbit(i))t+=len[i];
    return t;
}
void dfs(int pt,int fa){
    ans[pt][0]=query(l1,a[pt]-1),ans[pt][1]=query(l2,a[pt]-1);
    add(l1,a[pt],1);add(l2,a[pt],1);
    for (int i=head[pt];i!=0;i=eg[i].next){
        int v=eg[i].to;
        if (v==fa)continue;
        dfs(v,pt);
    }
    add(l2,a[pt],-1);ans[pt][0]=query(l1,a[pt]-1)-ans[pt][0];
}
int main(){
	freopen ("treesfruits.in","r",stdin);
	freopen ("treesfruits.out","w",stdout);
	scanf("%d",&n);
	for (int i=2;i<=n;i++){
	    int x;scanf("%d",&x);add(x,i);
    }
    for (int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
    sort(b+1,b+n+1);m=unique(b+1,b+n+1)-b-1;
    for (int i=1;i<=n;i++){
        a[i]=lower_bound(b+1,b+m+1,a[i])-b;
        a[i]=m-a[i]+1;num[a[i]]++;
    }
    for (int i=1;i<=m;i++)num[i]+=num[i-1];
    dfs(1,0);
    for (int i=1;i<=n;i++)printf("%d %d %d\n",ans[i][0],num[a[i]]-ans[i][0]-1,ans[i][1]);
}