結果

問題 No.778 クリスマスツリー
ユーザー pekempeypekempey
提出日時 2018-12-25 00:04:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 667 bytes
コンパイル時間 1,812 ms
コンパイル使用メモリ 63,320 KB
実行使用メモリ 22,704 KB
最終ジャッジ日時 2023-10-26 03:07:36
合計ジャッジ時間 4,817 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,300 KB
testcase_01 AC 4 ms
8,944 KB
testcase_02 AC 5 ms
8,944 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n;
vector<int> g[200000];

int bit[200001];
long long ans;

void update(int k, int v) {
    for (int i = k + 1; i <= 200000; i++) {
        bit[i] += v;
    }
}

int query(int k) {
    int res = 0;
    for (int i = k; i > 0; i -= i & -i) {
        res += bit[i];
    }
    return res;
}

void dfs(int u) {
    ans += query(u);
    update(u, 1);
    for (int v : g[u]) {
        dfs(v);
    }
    update(u, -1);
}

int main() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int a;
        cin >> a;
        g[a].push_back(i);
    }
    dfs(0);
    cout << ans << endl;
}

0