結果

問題 No.778 クリスマスツリー
ユーザー tottoripapertottoripaper
提出日時 2018-12-25 00:45:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,210 bytes
コンパイル時間 1,683 ms
コンパイル使用メモリ 166,996 KB
実行使用メモリ 21,184 KB
最終ジャッジ日時 2024-04-22 03:03:43
合計ジャッジ時間 2,620 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,600 KB
testcase_01 AC 4 ms
8,708 KB
testcase_02 AC 5 ms
8,752 KB
testcase_03 AC 4 ms
8,544 KB
testcase_04 AC 4 ms
8,660 KB
testcase_05 AC 4 ms
8,612 KB
testcase_06 AC 49 ms
21,116 KB
testcase_07 AC 22 ms
9,800 KB
testcase_08 AC 61 ms
14,784 KB
testcase_09 AC 67 ms
11,800 KB
testcase_10 AC 68 ms
11,900 KB
testcase_11 AC 65 ms
11,912 KB
testcase_12 AC 57 ms
12,000 KB
testcase_13 AC 38 ms
11,896 KB
testcase_14 AC 46 ms
21,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

struct FenwickTree{
    int numNode;
    std::vector<int> data;
    
    FenwickTree(int n) : numNode(n), data(n + 1, 0) {}

    void add(int k, int v){
        k += 1;
        
        while(k <= numNode){
            data[k] += v;
            k += k & -k;
        }
    }

    int sum(int k){
        k += 1;

        int res = 0;
        while(k > 0){
            res += data[k];
            k -= k & -k;
        }

        return res;
    }
};

int N;
std::vector<int> G[200100];
ll cnt;
FenwickTree ft(200100);

void dfs(int v){
    cnt += ft.sum(v);

    ft.add(v, +1);

    for(int w : G[v]){
        dfs(w);
    }

    ft.add(v, -1);
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N;

    for(int i=1;i<=N-1;++i){
        int p;
        std::cin >> p;

        G[p].emplace_back(i);
    }

    dfs(0);

    std::cout << cnt << std::endl;
}
0