1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<bits/stdc++.h>
using namespace std; typedef long long ll; typedef vector<int> v1; const int bit = 30; const int maxn = 1e5 + 5;
v1 tree[maxn], kdisSon[maxn]; int value[maxn], stk[maxn]; unsigned long long dp[maxn], ans[maxn]; int state[maxn][4];
void dfs1(int cur, int k, int dep) { stk[dep] = cur; if (dep - k >= 0) { kdisSon[stk[dep - k]].push_back(cur); } for (int son:tree[cur]) { dfs1(son, k, dep + 1); } }
void dfs2(int cur, int j, int k) { for (int t = 0; t < 4; t++) { state[cur][t] = 0; } for (int son:tree[cur]) { dfs2(son, j, k);
for (int kson:kdisSon[son]) { int msk1 = (value[kson] & (1 << j)) >> j; int msk2 = (value[kson] & (1 << k)) >> k; int t = msk1 << 1 | msk2; state[cur][t]--; } for (int t = 0; t < 4; t++) { state[cur][t] += state[son][t]; } } int msk1 = (value[cur] & (1 << j)) >> j; int msk2 = (value[cur] & (1 << k)) >> k; int t = msk1 << 1 | msk2; state[cur][t]++; dp[cur] = 0; for (int t = 0; t <= 1; t++) { ll add = 1ll * state[cur][t ^ 3] * state[cur][t]; add <<= j + k; dp[cur] += add; } }
int main() { int n, k; scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) { scanf("%d", &value[i]); } for (int i = 2; i <= n; i++) { int fa; scanf("%d", &fa); tree[fa].push_back(i); }
dfs1(1, k, 0); for (int i = 0; i < bit; i++) { dfs2(1, i, i); for (int cur = 1; cur <= n; cur++) { ans[cur] += dp[cur]; } for (int j = i + 1; j < bit; j++) { dfs2(1, i, j); for (int cur = 1; cur <= n; cur++) { ans[cur] += dp[cur] * 2; } } } for (int i = 1; i <= n; i++) { printf("%llu\n", ans[i]); } }
|