結果

問題 No.778 クリスマスツリー
ユーザー t98slidert98slider
提出日時 2023-07-12 23:52:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 541 bytes
コンパイル時間 4,847 ms
コンパイル使用メモリ 228,936 KB
実行使用メモリ 27,228 KB
最終ジャッジ日時 2023-10-12 13:38:18
合計ジャッジ時間 6,254 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 57 ms
27,160 KB
testcase_07 AC 24 ms
9,524 KB
testcase_08 AC 78 ms
18,052 KB
testcase_09 AC 69 ms
11,872 KB
testcase_10 AC 73 ms
11,716 KB
testcase_11 AC 74 ms
11,648 KB
testcase_12 AC 67 ms
11,780 KB
testcase_13 AC 40 ms
11,784 KB
testcase_14 AC 55 ms
27,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    atcoder::fenwick_tree<int> fw(N);
    for(int i = 1; i < N; i++){
        int p;
        cin >> p;
        G[p].emplace_back(i);
    }
    long ans = 0;
    function<void(int)> dfs = [&](int v){
        ans += fw.sum(0, v);
        fw.add(v, 1);
        for(auto &&u : G[v]) dfs(u);
        fw.add(v, -1);
    };
    dfs(0);
    cout << ans << '\n';
}
0