結果

問題 No.778 クリスマスツリー
ユーザー face4face4
提出日時 2019-03-02 19:37:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 851 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 71,280 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2024-04-22 03:07:24
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,784 KB
testcase_01 AC 5 ms
8,832 KB
testcase_02 AC 5 ms
8,704 KB
testcase_03 AC 5 ms
8,736 KB
testcase_04 AC 4 ms
8,704 KB
testcase_05 AC 5 ms
8,752 KB
testcase_06 AC 83 ms
21,120 KB
testcase_07 AC 38 ms
9,896 KB
testcase_08 AC 109 ms
14,976 KB
testcase_09 AC 106 ms
11,880 KB
testcase_10 AC 102 ms
11,936 KB
testcase_11 AC 97 ms
11,876 KB
testcase_12 AC 101 ms
11,776 KB
testcase_13 AC 75 ms
11,776 KB
testcase_14 AC 85 ms
21,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

struct BIT{
    vector<int> node;
    int n;
    BIT(int siz){
        n = siz+1;
        node.resize(n, 0); 
    }

    void add(int i, int x){
        while(i < n){
            node[i] += x;
            i += i&-i;
        }
    }

    int sum(int i){
        int ret = 0;
        while(i > 0){
            ret += node[i];
            i -= i&-i;
        }
        return ret;
    }
};

BIT bit(200000);
vector<int> a[200000];
int n;
ll ans = 0;

void dfs(int cur){
    ans += bit.sum(cur+1);
    bit.add(cur+1, 1);
    for(int next : a[cur]){
        dfs(next);
    }
    bit.add(cur+1, -1);
}

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