結果

問題 No.778 クリスマスツリー
ユーザー betrue12betrue12
提出日時 2019-06-22 16:32:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,274 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 170,024 KB
実行使用メモリ 25,112 KB
最終ジャッジ日時 2023-08-04 04:48:36
合計ジャッジ時間 3,439 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,200 KB
testcase_01 AC 5 ms
9,132 KB
testcase_02 AC 6 ms
9,248 KB
testcase_03 AC 5 ms
9,260 KB
testcase_04 AC 6 ms
9,080 KB
testcase_05 AC 5 ms
9,256 KB
testcase_06 AC 88 ms
24,832 KB
testcase_07 AC 40 ms
10,260 KB
testcase_08 AC 133 ms
17,356 KB
testcase_09 AC 128 ms
12,416 KB
testcase_10 AC 126 ms
12,464 KB
testcase_11 AC 126 ms
12,540 KB
testcase_12 AC 117 ms
12,544 KB
testcase_13 AC 78 ms
12,412 KB
testcase_14 AC 86 ms
25,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct BIT {
    int n;
    vector<T> dat;

    BIT(int n=0){
        initialize(n);
    }

    void initialize(int nin){
        n = nin;
        dat.resize(n, 0);
    }

    T sum(int i){
        T s = 0;
        while(i >= 0){
            s += dat[i];
            i = (i & (i+1)) - 1;
        }
        return s;
    }

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

    void plus(int i, T x){
        while(i < n){
            dat[i] += x;
            i |= i+1;
        }
    }

    // a[0]+...+a[ret] >= x
    int lower_bound(T x){
        int ret = -1;
        int k = 1;
        while(2*k <= n) k <<= 1;
        for( ;k>0; k>>=1){
            if(ret+k < n && dat[ret+k] < x){
                x -= dat[ret+k];
                ret += k;
            }
        }
        return ret + 1;
    }
};

vector<int> child[200000];
BIT<int64_t> bit(200000);
int64_t ans = 0;

void dfs(int i){
    ans += bit.sum(i-1);
    bit.plus(i, 1);
    for(int j : child[i]) dfs(j);
    bit.plus(i, -1);
}

int main(){
    int N;
    cin >> N;
    for(int i=1; i<N; i++){
        int a;
        cin >> a;
        child[a].push_back(i);
    }
    dfs(0);
    cout << ans << endl;
    return 0;
}
0