結果

問題 No.778 クリスマスツリー
ユーザー betrue12betrue12
提出日時 2019-06-22 16:32:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,274 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 168,576 KB
実行使用メモリ 25,196 KB
最終ジャッジ日時 2024-04-22 03:08:18
合計ジャッジ時間 2,921 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,292 KB
testcase_01 AC 4 ms
9,432 KB
testcase_02 AC 4 ms
9,508 KB
testcase_03 AC 3 ms
9,520 KB
testcase_04 AC 4 ms
9,476 KB
testcase_05 AC 4 ms
9,472 KB
testcase_06 AC 87 ms
24,928 KB
testcase_07 AC 37 ms
10,552 KB
testcase_08 AC 104 ms
17,228 KB
testcase_09 AC 105 ms
12,808 KB
testcase_10 AC 99 ms
12,692 KB
testcase_11 AC 101 ms
12,712 KB
testcase_12 AC 95 ms
12,712 KB
testcase_13 AC 75 ms
12,596 KB
testcase_14 AC 85 ms
25,196 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