結果
| 問題 | No.778 クリスマスツリー | 
| コンテスト | |
| ユーザー |  t33f | 
| 提出日時 | 2018-12-25 22:13:34 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 112 ms / 2,000 ms | 
| コード長 | 599 bytes | 
| コンパイル時間 | 672 ms | 
| コンパイル使用メモリ | 75,284 KB | 
| 実行使用メモリ | 27,712 KB | 
| 最終ジャッジ日時 | 2024-10-14 01:53:23 | 
| 合計ジャッジ時間 | 2,207 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int n;
vector<int> children[200100];
int bit[200100];
long long dfs(int i) {
    long long sum = 0;
    for (int x = i+1; x <= n+1; x += x&-x) bit[x]++;
    for (int j : children[i]) {
        for (int x = j+1; x > 0; x -= x&-x) sum += bit[x];
        sum += dfs(j);
    }
    for (int x = i+1; x <= n+1; x += x&-x) bit[x]--;
    return sum;
}
int main () {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int p; cin >> p;
        children[p].push_back(i);
    }
    cout << dfs(0) << endl;
}
            
            
            
        