func_code_string: #include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC optimize( Ofast )   using namespace std;   namespace ywy {   inline int get() {     int n = 0;     char c;     while ((c = getchar()) || 23333) {       if (c >=  0  && c <=  9 ) break;       if (c ==  - ) goto s;     }     n = c -  0 ;     while ((c = getchar()) || 23333) {       if (c >=  0  && c <=  9 )         n = n * 10 + c -  0 ;       else         return (n);     }   s:     while ((c = getchar()) || 23333) {       if (c >=  0  && c <=  9 )         n = n * 10 - c +  0 ;       else         return (n);     }   }   typedef struct _b {     int dest;     int nxt;   } bian;   bian memchi[1000001];   int gn = 1, heads[500001], deep[500001], ance[500001][19], lg[500001];   inline void add(int s, int t) {     memchi[gn].dest = t;     memchi[gn].nxt = heads[s];     heads[s] = gn;     gn++;   }   int stk[500001];   vector<int> vec[500001];   int dfn[500001], gdfn = 1;   void dfs(int pt) {     dfn[pt] = gdfn;     gdfn++;     for (register int i = heads[pt]; i; i = memchi[i].nxt) {       deep[memchi[i].dest] = deep[pt] + 1;       dfs(memchi[i].dest);     }   }   long long dp[500001];   inline int lca(int a, int b) {     if (deep[a] > deep[b]) swap(a, b);     for (register int i = lg[deep[b]]; i >= 0; i--) {       if (deep[ance[b][i]] >= deep[a]) b = ance[b][i];     }     if (a == b) return (a);     for (register int i = lg[deep[a]]; i >= 0; i--) {       if (ance[a][i] != ance[b][i]) a = ance[a][i], b = ance[b][i];     }     return (ance[a][0]);   }   inline int cmp(const int &a, const int &b) { return (dfn[a] < dfn[b]); }   inline void solve(int x) {     int sp = 0;     long long sig = 0;     for (register int i = 1; i < vec[x].size(); i++) {       int dpb = deep[lca(vec[x][i - 1], vec[x][i])];       while (sp) {         int a = deep[lca(vec[x][stk[sp - 1]], vec[x][i])];         if (a < dpb) {           sig += (i - stk[sp - 1] - 1) * (long long)dpb;           break;         }         if (sp == 1) {           sig -= (stk[sp - 1] + 1) *                  (long long)deep[lca(vec[x][stk[sp - 1]], vec[x][i - 1])];         } else           sig -= (stk[sp - 1] - stk[sp - 2]) *                  (long long)deep[lca(vec[x][stk[sp - 1]], vec[x][i - 1])];         sp--;       }       if (!sp) sig += i * (long long)dpb;       stk[sp] = i - 1;       sp++;       dp[vec[x][i]] += sig;     }   }   void ywymain() {     deep[0] = -1;     int n = get(), rt = 0;     lg[0] = -1;     for (register int i = 1; i <= n; i++) {       int f = get();       ance[i][0] = f;       if (!f)         rt = i;       else         add(f, i);       lg[i] = lg[i - 1] + (i == (i & -i));     }     deep[rt] = 1;     dfs(rt);     for (register int i = 1; i <= n; i++) vec[deep[i]].push_back(i);     for (register int i = 1; i <= lg[n]; i++) {       for (register int j = 1; j <= n; j++)         ance[j][i] = ance[ance[j][i - 1]][i - 1];     }     for (register int i = 2; i <= n && vec[i].size(); i++) {       sort(vec[i].begin(), vec[i].end(), cmp);       for (register int j = 0; j < vec[i].size(); j++) {         dp[vec[i][j]] = dp[ance[vec[i][j]][0]] + deep[ance[vec[i][j]][0]];       }       solve(i);       reverse(vec[i].begin(), vec[i].end());       solve(i);     }     for (register int i = 1; i <= n; i++) printf( %lld  , dp[i]);   }   }  // namespace ywy   int main() {     ywy::ywymain();     return (0);   }   