結果

問題 No.778 クリスマスツリー
ユーザー kriiikriii
提出日時 2018-12-25 00:05:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 628 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 48,500 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2023-08-04 04:42:16
合計ジャッジ時間 2,116 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,128 KB
testcase_01 AC 4 ms
9,096 KB
testcase_02 AC 4 ms
9,116 KB
testcase_03 AC 5 ms
9,168 KB
testcase_04 AC 4 ms
9,096 KB
testcase_05 AC 5 ms
9,144 KB
testcase_06 AC 52 ms
21,760 KB
testcase_07 AC 27 ms
9,756 KB
testcase_08 AC 107 ms
15,516 KB
testcase_09 AC 99 ms
12,444 KB
testcase_10 AC 103 ms
12,504 KB
testcase_11 AC 103 ms
12,444 KB
testcase_12 AC 96 ms
12,484 KB
testcase_13 AC 44 ms
12,416 KB
testcase_14 AC 52 ms
21,716 KB
権限があれば一括ダウンロードができます

ソースコード

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+1);
    in(x+1,1);
    for (auto y : G[x]) go(y);
    in(x+1,-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