比赛 |
4043级2023省选练习赛1 |
评测结果 |
AAAAAAAAAA |
题目名称 |
树的果实 |
最终得分 |
100 |
用户昵称 |
HeSn |
运行时间 |
2.383 s |
代码语言 |
C++ |
内存使用 |
11.83 MiB |
提交时间 |
2023-03-03 20:54:29 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, f[MAXN], a[MAXN], b[MAXN], c[MAXN], ans[MAXN][3], num[MAXN], pos[MAXN], cnt;
vector<int> cd[MAXN];
map<int, int> mp;
int lowbit(int x) {
return x & -x;
}
void addb(int x, int w) {
for(int i = x; i <= n; i += lowbit(i)) {
b[i] += w;
}
}
int queryb(int x) {
int sum = 0;
for(int i = x; i >= 1; i -= lowbit(i)) {
sum += b[i];
}
return sum;
}
void addc(int x, int w) {
for(int i = x; i <= n; i += lowbit(i)) {
c[i] += w;
}
}
int queryc(int x) {
int sum = 0;
for(int i = x; i >= 1; i -= lowbit(i)) {
sum += c[i];
}
return sum;
}
void dfs(int x) {
addc(a[x], 1);
int y = queryc(a[x] - 1);
ans[x][2] = queryb(a[x]-1);
addb(a[x], 1);
for(int i = 0; i < cd[x].size(); i ++) {
dfs(cd[x][i]);
}
ans[x][1] = queryc(a[x] - 1) - y;
addb(a[x], -1);
ans[x][0] = a[x] - 1 - ans[x][1];
}
bool cmp(int x, int y) {
return a[x] > a[y];
}
signed main() {
freopen("treesfruits.in", "r", stdin);
freopen("treesfruits.out", "w", stdout);
cin >> n;
for(int i = 2; i <= n; i ++) {
cin >> f[i];
cd[f[i]].push_back(i);
}
for(int i = 1; i <= n; i ++) {
cin >> a[i];
pos[i] = i;
}
sort(pos + 1, pos + n + 1, cmp);
for(int i = 1; i <= n; i ++) {
a[pos[i]] = i;
}
dfs(1);
for(int i = 1; i <= n; i ++) {
cout << ans[i][1] << ' ' << ans[i][0] << ' ' << ans[i][2] << endl;
}
return 0;
}