結果

問題 No.778 クリスマスツリー
ユーザー pekempeypekempey
提出日時 2018-12-25 00:04:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 675 bytes
コンパイル時間 535 ms
コンパイル使用メモリ 63,180 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2024-04-22 03:02:33
合計ジャッジ時間 1,961 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,064 KB
testcase_01 AC 4 ms
8,064 KB
testcase_02 AC 3 ms
8,060 KB
testcase_03 AC 4 ms
8,064 KB
testcase_04 AC 4 ms
8,064 KB
testcase_05 AC 4 ms
7,936 KB
testcase_06 AC 83 ms
21,248 KB
testcase_07 AC 38 ms
9,504 KB
testcase_08 AC 102 ms
15,004 KB
testcase_09 AC 109 ms
12,032 KB
testcase_10 AC 96 ms
12,032 KB
testcase_11 AC 97 ms
11,968 KB
testcase_12 AC 95 ms
12,032 KB
testcase_13 AC 72 ms
11,904 KB
testcase_14 AC 84 ms
21,228 KB
権限があれば一括ダウンロードができます

ソースコード

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 += i & -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