結果

問題 No.778 クリスマスツリー
ユーザー ks115ks115
提出日時 2020-05-05 21:45:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,128 bytes
コンパイル時間 1,565 ms
コンパイル使用メモリ 169,356 KB
実行使用メモリ 27,352 KB
最終ジャッジ日時 2023-09-09 11:57:38
合計ジャッジ時間 3,379 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 91 ms
27,260 KB
testcase_07 AC 38 ms
9,420 KB
testcase_08 WA -
testcase_09 AC 121 ms
11,600 KB
testcase_10 AC 124 ms
11,688 KB
testcase_11 AC 124 ms
11,636 KB
testcase_12 AC 117 ms
11,580 KB
testcase_13 AC 76 ms
11,568 KB
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
    int 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