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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| #include <bits/stdc++.h>
#define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back
using namespace std;
typedef long long LL; typedef pair<int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
inline void proc_status() { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl; }
template <typename T> T read () { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
const int Maxn = 5e5 + 100;
int N, fa[Maxn], root, dep[Maxn], dfn[Maxn], dfs_clock, size[Maxn], son[Maxn], top[Maxn]; int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];
inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }
vector <int> vec[Maxn]; int Maxdep;
inline void dfs (int x) { dep[x] = dep[fa[x]] + 1, size[x] = 1; Chkmax(Maxdep, dep[x]); vec[dep[x]].pb(x); for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; dfs(y); size[x] += size[y]; if (size[y] > size[son[x]]) son[x] = y; } }
inline void dfs (int x, int now) { dfn[x] = ++dfs_clock, top[x] = now; if (son[x]) dfs(son[x], now); for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; if (y == fa[x] || y == son[x]) continue; dfs(y, y); } }
LL Ans[Maxn];
namespace SEG { #define ls (root << 1) #define rs (root << 1 | 1) #define lson ls, l, mid #define rson rs, mid + 1, r struct tree { LL sum, tag; } Tree[Maxn << 2];
inline void push_up (int root) { Tree[root].sum = Tree[ls].sum + Tree[rs].sum; } inline void push_down (int root, int l, int r, int mid) { if (!Tree[root].tag) return ; Tree[ls].tag += Tree[root].tag, Tree[rs].tag += Tree[root].tag; Tree[ls].sum += Tree[root].tag * (mid - l + 1), Tree[rs].sum += Tree[root].tag * (r - mid); Tree[root].tag = 0; }
inline void update (int root, int l, int r, int x, int y, int z) { if (x <= l && r <= y) { Tree[root].sum += 1ll * (r - l + 1) * z; Tree[root].tag += z; return ; } int mid = l + r >> 1; push_down (root, l, r, mid); if (x <= mid) update (lson, x, y, z); if (y > mid) update (rson, x, y, z); push_up (root); }
inline LL query (int root, int l, int r, int x, int y) { if (x <= l && r <= y) return Tree[root].sum; int mid = l + r >> 1; LL ans = 0; push_down(root, l, r, mid); if (x <= mid) ans += query (lson, x, y); if (y > mid) ans += query (rson, x, y); return ans; }
#undef ls #undef rs #undef lson #undef rson }
inline void Update (int x) { while (x) { SEG :: update (1, 1, N, dfn[top[x]], dfn[x], 1); x = fa[top[x]]; } }
inline LL Query (int x) { LL ans = 0; while (x) { ans += SEG :: query (1, 1, N, dfn[top[x]], dfn[x]); x = fa[top[x]]; } return ans; }
inline void Solve () { dfs(root); dfs(root, root); for (int i = 1; i <= Maxdep; ++i) { for (int j = 0; j < vec[i].size(); ++j) Update (vec[i][j]); for (int j = 0; j < vec[i].size(); ++j) Ans[vec[i][j]] = Query (vec[i][j]); } for (int i = 1; i <= N; ++i) printf("%lld ", Ans[i] - dep[i]); }
inline void Input () { N = read<int>(); for (int i = 1; i <= N; ++i) fa[i] = read<int>(), add_edge (fa[i], i), (!fa[i]) ? (root = i) : 0; }
int main() { #ifndef ONLINE_JUDGE freopen("chuyu.in", "r", stdin); freopen("chuyu.out", "w", stdout); #endif Input(); Solve(); return 0; }
|