結果

問題 No.778 クリスマスツリー
ユーザー ks115ks115
提出日時 2020-05-05 21:44:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,129 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 170,168 KB
実行使用メモリ 27,468 KB
最終ジャッジ日時 2023-09-09 11:55:01
合計ジャッジ時間 3,855 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 91 ms
27,300 KB
testcase_07 AC 39 ms
9,336 KB
testcase_08 WA -
testcase_09 AC 107 ms
11,620 KB
testcase_10 AC 106 ms
11,928 KB
testcase_11 AC 104 ms
11,724 KB
testcase_12 AC 106 ms
11,724 KB
testcase_13 AC 76 ms
11,604 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;
};

int 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