結果

問題 No.778 クリスマスツリー
ユーザー kriiikriii
提出日時 2018-12-25 00:05:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 628 bytes
コンパイル時間 481 ms
コンパイル使用メモリ 49,792 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-04-22 03:02:20
合計ジャッジ時間 1,665 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,448 KB
testcase_01 AC 5 ms
8,448 KB
testcase_02 AC 5 ms
8,448 KB
testcase_03 AC 5 ms
8,448 KB
testcase_04 AC 5 ms
8,448 KB
testcase_05 AC 4 ms
8,448 KB
testcase_06 AC 50 ms
22,656 KB
testcase_07 AC 28 ms
10,532 KB
testcase_08 AC 66 ms
16,256 KB
testcase_09 AC 69 ms
13,056 KB
testcase_10 AC 63 ms
13,184 KB
testcase_11 AC 67 ms
13,184 KB
testcase_12 AC 63 ms
13,184 KB
testcase_13 AC 42 ms
12,928 KB
testcase_14 AC 48 ms
22,656 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