結果

問題 No.778 クリスマスツリー
ユーザー kriiikriii
提出日時 2018-12-25 00:04:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 622 bytes
コンパイル時間 472 ms
コンパイル使用メモリ 48,880 KB
実行使用メモリ 16,388 KB
最終ジャッジ日時 2023-10-26 03:07:47
合計ジャッジ時間 7,019 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>
using namespace std;

int B[200200];
int N,P[200200];
vector<int> G[200200];
long long ans;

void in(int x, int p)
{
    while (x <= N){
        B[x] += p;
        x +=  x & (-x);
    }
}

int out(int x)
{
    int r = 0;
    while (x){
        r += B[x];
        x -= x & (-x);
    }
    return r;
}

void go(int x){
    ans += out(x);
    in(x,1);
    for (auto y : G[x]) go(y);
    in(x,-1);
}

int main()
{
    scanf ("%d",&N);
    for (int i=1;i<N;i++){
        scanf ("%d",&P[i]);
        G[P[i]].push_back(i);
    }
    
    go(0);
    printf ("%lld\n",ans);
    
    return 0;
}
0