結果

問題 No.778 クリスマスツリー
ユーザー potoooooooopotoooooooo
提出日時 2019-01-01 22:07:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 913 bytes
コンパイル時間 1,668 ms
コンパイル使用メモリ 170,284 KB
実行使用メモリ 31,284 KB
最終ジャッジ日時 2024-04-22 03:04:56
合計ジャッジ時間 3,584 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 99 ms
31,272 KB
testcase_07 AC 43 ms
10,392 KB
testcase_08 AC 159 ms
20,420 KB
testcase_09 AC 149 ms
12,640 KB
testcase_10 AC 145 ms
12,644 KB
testcase_11 AC 149 ms
12,604 KB
testcase_12 AC 138 ms
12,656 KB
testcase_13 AC 85 ms
12,724 KB
testcase_14 AC 100 ms
31,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// one-based numbering
template<class T> struct Fenwick {
    vector<T> bit; int N;
    Fenwick(int n) {
      N = n;
      bit.resize(n+1, 0);
    }
    void add(int a, T w) {
      for (int x = a; x <= N; x += x & -x) bit[x] += w;
    }
    T sum(int a) {
      T ret = 0;
      for (int x = a; x > 0; x -= x & -x) ret += bit[x];
      return ret;
    }
};

long long dfs(int now, Fenwick<long long> &f, vector<int> v[]) {
    long long ret = 0;
    ret += f.sum(now+1); f.add(now+1, 1);
    for (int i = 0; i < (int) v[now].size(); i++) {
        ret += dfs(v[now][i], f, v);
    }
    f.add(now+1, -1);
    return ret;
}

int main() {
    int n; cin >> n;
    vector<int> v[n];
    for (int i = 1; i < n; i++) {
        int k; cin >> k;
        v[k].push_back(i);
    }
    Fenwick<long long> fenwick(n+1);
    cout << dfs(0, fenwick, v) << endl;
    return 0;
}
0