結果

問題 No.778 クリスマスツリー
ユーザー veqccveqcc
提出日時 2019-02-15 21:54:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 952 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 96,144 KB
実行使用メモリ 45,340 KB
最終ジャッジ日時 2024-04-22 03:07:19
合計ジャッジ時間 2,367 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
28,672 KB
testcase_01 AC 11 ms
29,228 KB
testcase_02 AC 11 ms
28,928 KB
testcase_03 AC 11 ms
29,668 KB
testcase_04 AC 11 ms
28,808 KB
testcase_05 AC 11 ms
29,068 KB
testcase_06 AC 61 ms
45,340 KB
testcase_07 AC 31 ms
30,796 KB
testcase_08 AC 113 ms
36,920 KB
testcase_09 AC 101 ms
32,512 KB
testcase_10 AC 102 ms
32,580 KB
testcase_11 AC 106 ms
31,972 KB
testcase_12 AC 97 ms
33,392 KB
testcase_13 AC 49 ms
32,072 KB
testcase_14 AC 59 ms
44,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int n, bit[1 << 20];
vector<int> children[1 << 20];

int sum(int i) {
    int s = 0;
    while (i > 0) {
        s += bit[i];
        i -= i & -i; // 最後の1ビット
    }
    return s;
}

void add(int i, int x) {
    while (i <= n) {
        bit[i] += x;
        i += i & -i;
    }
}

ll dfs(int i) {
    ll sm = 0LL;
    add(i+1, 1);
    for (int j: children[i]) {
        sm += sum(j+1);
        sm += dfs(j);
    }
    add(i+1, -1);
    return sm;
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int p;
        cin >> p;
        children[p].push_back(i);
    }

    cout << dfs(0) << "\n";
    return 0;
}
0