結果

問題 No.778 クリスマスツリー
ユーザー ks115ks115
提出日時 2020-05-05 22:02:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,127 bytes
コンパイル時間 1,621 ms
コンパイル使用メモリ 168,444 KB
実行使用メモリ 27,268 KB
最終ジャッジ日時 2023-09-09 12:18:51
合計ジャッジ時間 3,682 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 93 ms
27,268 KB
testcase_07 AC 38 ms
9,516 KB
testcase_08 AC 111 ms
17,748 KB
testcase_09 AC 104 ms
11,712 KB
testcase_10 AC 104 ms
11,676 KB
testcase_11 AC 104 ms
11,780 KB
testcase_12 AC 100 ms
11,936 KB
testcase_13 AC 77 ms
11,864 KB
testcase_14 AC 92 ms
27,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

template <typename T>
class BIT {
   public:
    BIT(int n) : N(n), A(n + 1, 0) {}

    T sum(int i) {
        T res = 0;
        while (i > 0) {
            res += A[i];
            i -= i & (-i);
        }
        return res;
    }

    T sum(int i, int j) {
        return sum(j) - sum(i - 1);
    }

    void add(int i, T x) {
        while (i <= N) {
            A[i] += x;
            i += (i & (-i));
        }
    }

   private:
    vector<T> A;
    int N;
};

ll dfs(BIT<int> &B, vector<vector<int>> &G, int n) {
    // cout << "# looking at : " << n << endl;
    B.add(n + 1, 1);
    ll ret = 0;
    for (auto g : G[n]) {
        ret += B.sum(g + 1);
        ret += dfs(B, G, g);
    }
    B.add(n + 1, -1);
    return ret;
}

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    REP(i, 1, N) {
        int a; cin >> a;
        G[a].push_back(i);
    }

    BIT<int> bit(N);
    cout << dfs(bit, G, 0) << endl;

    return 0;
}
0